Skip to content
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

Add when clause context for language at active cursor #107989

Open
KamasamaK opened this issue Oct 2, 2020 · 7 comments
Open

Add when clause context for language at active cursor #107989

KamasamaK opened this issue Oct 2, 2020 · 7 comments
Assignees
Labels
feature-request Request for new features or functionality languages-basic Basic language support issues
Milestone

Comments

@KamasamaK
Copy link

KamasamaK commented Oct 2, 2020

I would like a new when clause context for the language ID at the active cursor position within the document. Right now, we have editorLangId which only uses the language ID of the editor itself, but documents can have other languages embedded in them defined in the contributes.grammars.

I'm not sure about the full extent of how the embedded language is used, but I've seen it used for some built-in features like language snippets and the built-in commands that toggle language comments. I can also see the language present in the inspector for the command editor.action.inspectTMScopes. Since it's clearly seen as useful for this built-in functionality and has implementations demonstrating how it can be used, it should also be clear that it would be useful to have access to this in the when clause.

@jrieken jrieken added this to the Backlog Candidates milestone Oct 3, 2020
@jrieken jrieken added feature-request Request for new features or functionality languages-basic Basic language support issues labels Oct 3, 2020
@jimpriest
Copy link

+1 for this feature.

@gjsjohnmurray
Copy link
Contributor

+1 for this feature.

I hope you upvoted the issue above, otherwise your +1 won't count and will only have been 'noise'.

@alexdima alexdima removed their assignment Nov 16, 2020
@KamasamaK KamasamaK changed the title Add when clause context for embedded language ID Add when clause context for language at active cursor Nov 24, 2020
@jtrakk
Copy link

jtrakk commented Feb 21, 2021

This feature is relevant to "literate programming" notebook formats like R Markdown and Weave.jl. The embedded language is specified with the markdown code-block syntax.

This is markdown.

```{python}
# This is python code.
import random
x = random.random()
```

```{r}
# This is R code.
x <- runif()
```

@hediet
Copy link
Member

hediet commented Oct 19, 2021

Can you provide some use-cases for this when clauses?

@jtrakk
Copy link

jtrakk commented Oct 19, 2021

In addition to the "literate programming" I mentioned above embedding languages in Markdown, reStructuredText, or LaTeX, there is also embedding HTML or SQL in ES6. The syntax highlighting and keybindings would be available for editing, formatting, and executing the embedded code with the appropriate language server and compiler/interpreter.

@hediet
Copy link
Member

hediet commented Oct 20, 2021

The syntax highlighting

But what does syntax highlighting have to do with the requested feature?

keybindings would be available for editing, formatting, and executing the embedded code

I think these keybinding handlers most likely also need to understand the surrounding markdown, at least for formatting and executing the code, so I suggest to have editorLangId == r || editorLangId == markdown (and make the command aware of markdown) instead of some hypothetical editorLangIdAtCursor == r. The only problem here is that you cannot bind multiple language-specific commands to the same key-binding, but I wonder if that really is a problem.

I doubt a handler that triggers when editorLangIdAtCursor == r would be able to work for code that is embedded in completely arbitrary/unexpected documents, as it does not know the semantics of the outer document. For example in this case, a "toggle line comment command" would need to be aware of the outer html:

<script type="r">print("Hello World!")</script>

In that case, the when clause should probably rather be editorLangId == r || (editorLangId == markdown && editorLangIdAtCursor == r).

@caleb531
Copy link

caleb531 commented Feb 15, 2024

A very practical use case is JSX/TSX files.

  1. When my cursor is at a bracket in JavaScript syntax, like a functional component (where language ID at cursor is javascriptreact or typescriptreact), I want my key binding to trigger the "Go to Bracket" command.
  2. However, if my cursor is currently at a JSX tag name (where the language ID at cursor is jsx-tags), then I want to trigger the Highlight Matching Tag extension's "Select matching tag pair" command)

Currently, I have two key bindings: one for bracket-matching, and one for HTML/JSX tag-matching. However, when both syntaxes are present in the same file, editorLangId is the same no matter what scope my cursor is at.

[
  // Unset default key binding
  {
    "key": "shift+cmd+\\",
    "command": "-editor.action.jumpToBracket",
    "when": "editorTextFocus"
  },
  // Key binding for brackets
  {
    "key": "alt+cmd+m",
    "command": "editor.action.jumpToBracket",
    "when": "editorTextFocus"
  },
  // Key binding for JSX tags specifically
  {
    "key": "alt+cmd+b",
    "command": "highlight-matching-tag.selectPairContents",
    "when": "editorLangId == 'handlebars' || editorLangId == 'html' || editorLangId == 'jsx-tags' || editorLangId == 'svelte' || editorLangId == 'xml'"
  }
]

The only workaround I've found is to assign separate key bindings: one for bracket-matching, and another for tag-matching. But of course, this is not ideal because of the added cognitive overhead in remembering which key binding to use.

Therefore, it would be very helpful if VS Code a editorLangIdAtCursor variable for When expressions.

caleb531 added a commit to caleb531/dotfiles that referenced this issue Feb 15, 2024
This is to work around an issue where a VS Code key binding can be
scoped to the current language ID of the editor, but not the current
language ID at the cursor. This makes working in JSX/TSX files more
annoying. For more information, see:
<microsoft/vscode#107989 (comment)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality languages-basic Basic language support issues
Projects
None yet
Development

No branches or pull requests

9 participants