question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Increase token granularity for improved syntax highlighting

See original GitHub issue

It would be cool to add more tokens so different parts of the code can be highlighted differently. Prism seems more granular than CodeMirror, but it’s still missing some nice tokens compared to Atom and VS Code.

For JavaScript, it would be nice to have these:

  • token parameter
function add(a, b) { // <-- `a` and `b` receive `parameter`
  return a + b;
}
  • token property definition and token property access
const obj = {
  example: true // <-- `example` receives `property` and `definition`
};
obj.example; // <-- `example` receives `property` and `access`
  • token function method
this.func(); // `func` receives `method` if being called on an object
  • token keyword module

All keywords here should receive the module token.

import { thing as thingy } from 'thing';
export default thingy;
  • token keyword special

“special”(?) keywords like this

  • token variable dom
window
document
navigator
performance
localStorage
  • token console
console.log('hello'); // <-- `console` receives this token
  • token operator spread
const copy = [...arr]; // <- `...` receives this token

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
RunDevelopmentcommented, Feb 14, 2019

How would I make "value" receive the string token?

This will come soon to CSS Extras with #1638 and #1671. The only difference is that I highlight your "value" not as string but as value (that’s the token name).

2reactions
RunDevelopmentcommented, Nov 21, 2018

Insert this code into prism-javascript.js in line 37 and you will have most of the features.

The code
Prism.languages.insertBefore('javascript', 'punctuation', {
	'definition': {
		pattern: /([{,]\s*)[a-z]\w*(?=\s*:)/i,
		lookbehind: true,
		alias: 'property'
	},
	'access': {
		pattern: /(\.\s*)[a-z]\w*/i,
		lookbehind: true,
		alias: 'property'
	},
	'variable ': {
		pattern: /\b(?:window|document|navigator|performance|localStorage)\b/,
		alias: 'dom'
	},
	'console': /\bconsole\b/,
	'spread': {
		pattern: /\.{3}/,
		alias: 'punctuation'
	}
});

But let me warn you:

  1. I did this quick and dirty, so it’s not perfect and no guarantees.
  2. It really doesn’t look nice IMO, you will probably have to adjust the default theme you’re using.

Keep in mind that if you want to use the minified file, you will have to rebuild Prism (refer to README.md).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizations in Syntax Highlighting, a Visual Studio Code Story
A tokenizer can store some state at the end of a tokenized line, which will be passed back when tokenizing the next line....
Read more >
New npm features for secure publishing and safe consumption
We are excited to announce two new features for a safer npm package ecosystem experience: granular access tokens and the npm code explorer....
Read more >
T6471 Replace zsh-syntax-highlighting with fast-syntax ...
Faster, better optimized and increased granularity with highlighting syntax. Is it open source? - Yes. Uses exactly the same license as the current...
Read more >
Custom syntax highlighting for certain keywords (i.e. access ...
IDEABKL-63 Improved control over syntax highlighting for keywords ... Yes but that's just how they decided to colorize the Java language tokens, ...
Read more >
User Manual - rust-analyzer
rust-analyzer: building a better Rust IDE. ... Semantic Syntax Highlighting; Show Syntax Tree; Shuffle Crate Graph; Status; Structural Search and Replace ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found