Skip to content

Can I get scope / scopeRange at a position? #580

Open

Description

From Bill Ticehurst (@billti) on November 1, 2015 6:10

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

Activity

  1. seanmcbreen commented on Nov 24, 2015

    @seanmcbreen
    Author

    From vilic on November 1, 2015 15:19

    👍

  2. seanmcbreen commented on Nov 24, 2015

    @seanmcbreen
    Author

    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.

  3. seanmcbreen commented on Nov 24, 2015

    @seanmcbreen
    Author

    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 a ITokenTypeClassificationSupport which can contribute a regex to classify words.

  4. seanmcbreen commented on Nov 24, 2015

    @seanmcbreen
    Author

    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?

  5. seanmcbreen commented on Nov 24, 2015

    @seanmcbreen
    Author

    From Johannes Rieken (@jrieken) on November 3, 2015 9:59

    unfortunately yes

  6. added
    feature-request
    on Nov 25, 2015
  7. hoovercj commented on May 14, 2016

    @hoovercj
    Member

    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.

    Is there any update on if/when we can expect a way to get the scopes at a position or offset?

  8. egamma commented on May 16, 2016

    @egamma
    Member

    Cody Hoover (@hoovercj) all I can currently say is that this is still on the backlog, sorry.

  9. 26 remaining items

  10. sandipchitale commented on Jul 1, 2022

    @sandipchitale

    I have implemented an extension:

    Show scopes at cursor in active editor

    showing how to use API exposed by:

    HyperScopes

    Hope this helps someone.

  11. m-paternostro commented on Jul 3, 2022

    @m-paternostro

    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.

  12. Zxynine commented on Jul 15, 2022

    @Zxynine

    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.

  13. pelmers commented on Aug 13, 2022

    @pelmers
    Contributor

    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?

  14. lukstafi commented on Nov 13, 2022

    @lukstafi

    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.

  15. Fred-Vatin commented on Nov 22, 2022

    @Fred-Vatin

    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.

  16. PoetaKodu commented on Dec 23, 2022

    @PoetaKodu

    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.

  17. iCSawyer commented on Feb 13, 2023

    @iCSawyer

    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?

  18. zm-cttae commented on Sep 8, 2023

    @zm-cttae

    https://github.com/vsce-toolroom/vscode-textmate-languageservice/releases/tag/v2.0.0

    • Add getTokenInformationAtPosition method for fast positional token polyfill: vscode.TokenInformation.
    • Add getScopeInformationAtPosition method to get Textmate token data: TextmateToken.
    • Add getScopeRangeAtPosition method to get token range: vscode.Range.
    • Add getLanguageConfiguration method for language configuration: LanguageDefinition.
    • Add getGrammarConfiguration method to get language grammar wiring: GrammarLanguageDefinition.
    • Add getContributorExtension method 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.

  19. starball5 commented on Dec 30, 2024

    @starball5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apifeature-requesttokenization

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions