highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

(Typescript) highlight obvious user defined type names (`interface`, etc)

Open amirqasemi74 opened this issue 3 years ago • 9 comments

See this example: image as you can see in boudingRect params the Rect type has not been highlighted. also interface name. I think these one should be highlighted

amirqasemi74 avatar Jul 12 '21 16:07 amirqasemi74

This is because we are not a full TypeScript parser (nor do we desire to be), we're more of a "fancy pattern matcher". As such it's impossible to know that Rect[] there is a type... For some languages we can examine the immediate context but in this case that is also insufficient. : is used in many places in JS and ) isn't helpful.

Note: The [] in this case is strange, but even if that were a strong signal that would only allow us to detect Blah[] types, not all types in general, and therefore it'd be even more confusing - vs now where we highlight the "standard" types always, and simply do not highlight user types.


I'd love to see someone attempt a plugin/grammar pairing that hooked up an entirely different parser - with our HTML generation. https://github.com/codemirror/lang-javascript would be a good choice I think. If you're interested in a project like that, let us know.

joshgoebel avatar Jul 13 '21 11:07 joshgoebel

Rules could be added for the simpler cases such as interface [type]... this would be a perfect use for our new multi-class matcher support... but that doesn't help with params or types in other contexts.

joshgoebel avatar Jul 13 '21 11:07 joshgoebel

Rules could be added for the simpler cases such as interface [type]...

Tagging as "good first issue" as these simple cases shouldn't be too hard and there are examples from other modern grammars, like Wren.

joshgoebel avatar Jul 23 '21 00:07 joshgoebel

Also worth exploring if this could somehow just be handled by a general CamelCaseIsAClass rule...

joshgoebel avatar Aug 29 '21 19:08 joshgoebel

@joshgoebel I can pick this up, but will need some guidance as to which existing examples/files I can refer to and how I should approach this

deepto98 avatar Oct 09 '21 21:10 deepto98

I dunno about samples. I think start might be just a CamelCaseIsAClass as mentioned above which we already have examples of in Ruby and JS I think?

joshgoebel avatar Oct 10 '21 02:10 joshgoebel

I have a possible solution that I can link -- I noticed the CamelCaseAsAClass behavior and attempted to fix it based on looking for any titles following "interface." Right now, I only have an implementation for interfaces, but if everything looks good I can implement this for other instances of user-defined types or variables

nbeck415 avatar Dec 17 '21 22:12 nbeck415

Hi @joshgoebel, I want to fix this issue and I have a suggested solution to this issue this is the result I have reached now. I want to know what I should do else edit the typescript file and update the markup tests to make a valid solution.

image

Thanks in advance,

MohamedAli00949 avatar Oct 26 '23 00:10 MohamedAli00949

I have a suggested solution

What is your suggested solution? Just make a PR with your changes and we'll have a look.

update the markup tests to make a valid solution.

You can edit the existing tests or add new ones... if you look at the markup test js file itself you can uncomment a line to have it write the expectations for you, but you should still read the result to make sure it's correct... the developer tool in debug mode is also super helpful for this kind of thing.

joshgoebel avatar Oct 26 '23 00:10 joshgoebel