You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The API call document.getWordRangeAtPosition(position) appears to use its own definition of a word. For example, my tmLanguage defines attrib-name as a token/scope, yet getWordRangeAtPosition appears to break this into 2 words on the - character.
How can I get token ranges at a position based on my custom syntax? (And it would be really useful if I could get the scope name that goes along with it too).
Copied from original issue: Microsoft/vscode-extensionbuilders#76
Reacted by CJ Bell, vs, Timon van Spronsen, AB, Guodong Shen, CoenraadS, Geoff, Shawn, Jens Hausdorf, Fred Vatin and 80 more
Bill Ticehurst (@billti) despite the lack of access to scopes you can define your a custom word definition such that it will be picked up by document.getWordRangeAtPosition. You can register a ITokenTypeClassificationSupport which can contribute a regex to classify words.
Thanks Johannes Rieken (@jrieken) , I spotted that, and it may be a useful interim solution. But generally for now, if I want to know the classification accurately for a position in a CFG, seems I'll need to document.getText() and run my own parser over it - is that right?
Another informal vote for this feature, if I may...
In our case, we want to correlate the content from the editor to an extremely rich repository of runtime-produced information. Without the minimum understanding of the code (precisely what symbols/scope/tokens would provide), such a correlation is faulty way more often than acceptable.
Reacted by Roberto Huertas, David L. Goldberg and Francisco Boni
I would love this feature too, I keep ending up here trying to find a way to know for sure if a given position is in a comment or not, it seems like without regex and reading of a language config file there is no clear way to know that. Even if I wasnt just looking for a way to know if something is within a comment, this feature is still something I want to see implemented and would make a world of difference to many extensions.
Reacted by Dave, Marcelo Paternostro, Felix, Peter Elmers, Fred Vatin, Christos Giamouridis, David L. Goldberg and Chrysaloid
Like Zxynine, I am also looking for a way to tell whether a selection in the editor is a comment. Apparently we are not the only ones. I found this commit in Better Comments which defines the line comment format for many languages: aaron-bond/better-comments@47717e7
So that's one way to implement this (at least for line comments), though not my favorite.
Of course these are all workarounds, and I think VS Code should provide access to this information. It knows it already, after all!
I agree with Andrew Dupont (@savetheclocktower) that the only given justification doesn't seem adequate. Can we quantify the risk? How big of a change has happened to Textmate grammars in the last 7 years?
Reacted by Tom, Andrew Dupont, Dave, LucasFA, Marcelo Paternostro, Gábor, Christos Giamouridis, David L. Goldberg and Chrysaloid
If vscode.provideDocumentRangeSemanticTokens was outputting all tokens for most languages, it would satisfy many of the needs discussed here. But from my limited experiments, it only outputs "interesting" tokens, it doesn't output tokens for comments, string literals, operators.
Great. I cannot access information about my documents that are there, hidden behind VS Code wall. How is this a thing in 2022? Admins, do not ignore that please.
Activity
seanmcbreen commentedon Nov 24, 2015
From vilic on November 1, 2015 15:19
👍
seanmcbreen commentedon Nov 24, 2015
From Erich Gamma (@egamma) on November 2, 2015 8:16
Exposing the scope names in the API is on the backlog, but will not make it into the November update.
seanmcbreen commentedon Nov 24, 2015
From Johannes Rieken (@jrieken) on November 2, 2015 18:16
Bill Ticehurst (@billti) despite the lack of access to scopes you can define your a custom word definition such that it will be picked up by
document.getWordRangeAtPosition. You can register aITokenTypeClassificationSupportwhich can contribute a regex to classify words.seanmcbreen commentedon Nov 24, 2015
From Bill Ticehurst (@billti) on November 2, 2015 19:3
Thanks Johannes Rieken (@jrieken) , I spotted that, and it may be a useful interim solution. But generally for now, if I want to know the classification accurately for a position in a CFG, seems I'll need to
document.getText()and run my own parser over it - is that right?seanmcbreen commentedon Nov 24, 2015
From Johannes Rieken (@jrieken) on November 3, 2015 9:59
unfortunately yes
hoovercj commentedon May 14, 2016
Is there any update on if/when we can expect a way to get the scopes at a position or offset?
egamma commentedon May 16, 2016
Cody Hoover (@hoovercj) all I can currently say is that this is still on the backlog, sorry.
26 remaining items
sandipchitale commentedon Jul 1, 2022
I have implemented an extension:
Show scopes at cursor in active editor
showing how to use API exposed by:
HyperScopes
Hope this helps someone.
m-paternostro commentedon Jul 3, 2022
Another informal vote for this feature, if I may...
In our case, we want to correlate the content from the editor to an extremely rich repository of runtime-produced information. Without the minimum understanding of the code (precisely what symbols/scope/tokens would provide), such a correlation is faulty way more often than acceptable.
Zxynine commentedon Jul 15, 2022
I would love this feature too, I keep ending up here trying to find a way to know for sure if a given position is in a comment or not, it seems like without regex and reading of a language config file there is no clear way to know that. Even if I wasnt just looking for a way to know if something is within a comment, this feature is still something I want to see implemented and would make a world of difference to many extensions.
pelmers commentedon Aug 13, 2022
Like Zxynine, I am also looking for a way to tell whether a selection in the editor is a comment. Apparently we are not the only ones. I found this commit in Better Comments which defines the line comment format for many languages: aaron-bond/better-comments@47717e7
So that's one way to implement this (at least for line comments), though not my favorite.
I have also tried the API exposed by Hyperscopes (https://github.com/draivin/hscopes), but I experienced multi-second freezes of the extension host even when editing very modestly sized files.
Perhaps tree-sitter would be fast enough to parse files without delay. I see the extensions https://github.com/georgewfraser/vscode-tree-sitter and https://github.com/EvgeniyPeshkov/syntax-highlighter import web-tree-sitter (wasm-compiled tree sitter modules) to provide syntax highlighting.
Of course these are all workarounds, and I think VS Code should provide access to this information. It knows it already, after all!
I agree with Andrew Dupont (@savetheclocktower) that the only given justification doesn't seem adequate. Can we quantify the risk? How big of a change has happened to Textmate grammars in the last 7 years?
lukstafi commentedon Nov 13, 2022
If
vscode.provideDocumentRangeSemanticTokenswas outputting all tokens for most languages, it would satisfy many of the needs discussed here. But from my limited experiments, it only outputs "interesting" tokens, it doesn't output tokens for comments, string literals, operators.Fred-Vatin commentedon Nov 22, 2022
Seven years this issue is open.
SEVEN YEARS !!!
I don’t expect this will be fixed soon. We’ll have to learn to live with it or move to another IDE.
PoetaKodu commentedon Dec 23, 2022
Great. I cannot access information about my documents that are there, hidden behind VS Code wall. How is this a thing in 2022? Admins, do not ignore that please.
iCSawyer commentedon Feb 13, 2023
EIGHT years! Eight years after eight years, do you know how I've spent the last eight years? Why is it so difficult to provide them?
zm-cttae commentedon Sep 8, 2023
https://github.com/vsce-toolroom/vscode-textmate-languageservice/releases/tag/v2.0.0
getTokenInformationAtPositionmethod for fast positional token polyfill:vscode.TokenInformation.getScopeInformationAtPositionmethod to get Textmate token data:TextmateToken.getScopeRangeAtPositionmethod to get token range:vscode.Range.getLanguageConfigurationmethod for language configuration:LanguageDefinition.getGrammarConfigurationmethod to get language grammar wiring:GrammarLanguageDefinition.getContributorExtensionmethod to get extension source of language ID:vscode.Extension.Please star the project on GitHub if you think there is further use you could make of it.
starball5 commentedon Dec 30, 2024
Related on Stack Overflow: Get grammar scopes / tokens in vscode extension