Skip to content

Add syntax highlighting to REPL input #1150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Gorthog opened this issue Nov 10, 2020 · 4 comments
Open

Add syntax highlighting to REPL input #1150

Gorthog opened this issue Nov 10, 2020 · 4 comments
Labels
enhancement you can do this Good candidate for a pull request.

Comments

@Gorthog
Copy link

Gorthog commented Nov 10, 2020

Desired Behavior

ts-node currently supports output highlighting, but not input:
image

Additional context

The next REPL of nodejs - node-prototype-repl - already supports syntax highlighting for js. It uses emphasize. If we change the following line:

https://github.com/nodejs/repl/blob/db95cfb1a177b056b8bc257dfd62c7b0e50134df/src/highlight.js#L44 to:

emphasize.highlight('ts', s, sheet).value;

We can get typescript syntax highlighting for node REPL:

image

For additional details see #1144.

@cspotcode cspotcode added enhancement you can do this Good candidate for a pull request. labels Nov 10, 2020
@cspotcode
Copy link
Collaborator

Labelled "help wanted" to indicate we will wait for a pull request.

@erana111
Copy link

erana111 commented May 5, 2021

@cspotcode @sinapis I can help. I will create a PR with the suggested change

@erana111
Copy link

erana111 commented May 5, 2021

@sinapis what is needed to be done for this to be implemented?

@cspotcode
Copy link
Collaborator

cspotcode commented Mar 19, 2022

I stumbled upon some TS compiler API functionality that's relevant here:

const classifier = ts.createClassifier();
const classifications = classifier.getEncodedLexicalClassifications('const a = 123;', ts.EndOfLineState.None);
for(let i = 0; i < classifications.spans.length; i += 3) {
  const [start, length, classification] = classifications.spans.slice(i, i + 3);
  console.log(start, length, ts.ClassificationType[classification]);
}
0 5 keyword
6 1 identifier
8 1 operator
10 3 numericLiteral
13 1 punctuation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement you can do this Good candidate for a pull request.
Projects
None yet
Development

No branches or pull requests

3 participants