visual studio code add corresponding import statements for snippets - import

I tried to create custom snippets for my extension in vscode.
But to use those snippets I also need specific imports statements, I'm trying out to figure out how to add corresponding import statements while a snippet is selected from choices.?
Does vscode provide a way to do this?

If your programming language supports the "Optimize Imports" command, you can take advantage of it to get close to your desired behavior. By default it is set to the shift+opt+O keybinding in vscode.
In JS/TS the "Optimize Imports" command will move an import to the top of a file no matter what line it's written on, as long as it's a syntactically valid import, i.e., not inside a function, etc.
Option 1
You could make your snippet more convenient by ending it on the importable keyword. For example, with a React component snippet that you might need to import, you can use $0 to return to the keyword after supplying the additional content. This would allow you to immediately type cmd+. to "Quick Fix" the import.
{
"AuthorCard": {
"prefix": "ac",
"body": [
"<AuthorCard$0>",
" $1",
"</AuthorCard>"
]
},
}
Option 2
If your function snippet is intended to be inserted into the global scope of the file, then you can include the import in the snippet itself. Then, immediately "Optimize Imports" and it will send the import statement to the top of the file.
For example, the snippet could look like the following.
"blah": {
"prefix": "blah",
"body": [
"import blah from 'blah'",
"export function myBlah() {",
" return blah.doBlah()",
"}"
]
},
Option 3
If you snippet is intended to be used embedded within other scopes within the file, then create a second snippet of the just the import. E.g., for a snippet blah, the snippet iblah could import all the required dependencies. Now, you only need a "quick" way to get to a valid scope for the imports and then back to the place you started, both of which are possible in vscode. I will mention the default keybindings, but, for the record, you can rebind the underlying commands to whatever you'd like.
To get to a valid scope for your import snippet, you have multiple options. The best options is probably cmd+up, which will take you to the top of the file. Other options are shift+cmd+\, which takes you to the closing bracket of a statement, and cmd+enter, which takes you to a new line. From a valid scope you can trigger your import snippet and then "Optimize Imports" with shift+opt+O.
Lastly, to return to your original function, you can "Go Back" in vscode which defaults to ctrl+-, which returns your to your last cursor position.
These options might be less than ideal, especially if you are intending these to be public snippets for an extension package, but, still, they are a collection of convenient tricks that might provide useful (and answer the "spirit" of your question).

Related

VSCode Intellisense: can we add keywords to an "exclude" list that won't be auto-completed?

I'm writing C code in VSCode with the amazing Tab Out extension that let you tab out of brackets, quotes etc. Together with the automatically added end brackets that VSCode has,I feel very comfortable writing C code when I can just do tab, tab, semi-conlon to end a line. However, every time I try to write self at the end near the ), I can't tab out without it being changed to STDERR_FILENO due to autocompletion.
How can I add a word, such as self, so that Intellisense will not autocomplete it? Is there a list of "excluded" words that I can tell Intellisense to not mess with?
I don't want to turn off any of the autocomplete stuff, I just want some very specific keywords to not be replaced when I tab out of stuff. I also don't want to stop using tab to autocomplete. these features are all very handy.
My current work-around is defining a new code snippet in the C snippet file:
"self": {
"body": "self",
"prefix": "self",
"description": "this is to prevent stupid suggestions for the keyword self"
}
but I consider this an ugly fix so let me know if you can help!

Variable name autocomplete for VSCode Language Extension (GameMaker / GML files)?

I'm editing GML files (GameMaker Studio) in VSCode. There's a wonderful plugin, GML Support which adds autocomplete for inbuilt GML functions and instances variables along with a bunch of other cool things.
However, VSCode doesn't seem to recognise local variables in GML (see screen grab below. Dot notation works fine)
I had a look at the VSCode's Programmatic Language Extension for variable name auto-completion but still don't get how I could register the variable declaration (i.e. var fooBar = 23;) with VSCode's Language Server.
Ideally, I'd like the Language Server to respect variable scope for GML files:
global variables - any var declarations for files under script folder
any local variable declarations - all var declarations in the surrounding {...}
What would be the easiest way to add variable name completion as described above?
Thanks in advance!
Edit: looked at vscode-python to see how registerCompletionItemProvider (based on VSCode Language Extension doco) could be used. Unfortunately, still not clear to me as vscode-python seem to rely on Jedi to provide symbols?
So any points appreciated!
If you want to enable simple auto-completion, you can add the following to your settings.json (Command Palette ➜ Open Settings (JSON)):
"[gml-gms81]": { "editor.quickSuggestions": true },
"[gml-gms1]": { "editor.quickSuggestions": true },
"[gml-gms2]": { "editor.quickSuggestions": true },
which works for a workaround:
For a proper solution, well, you'll need to use the registerCompletionItemProvider and index the file on demand or as you go.
The official example demonstrates the use.
For intricacies of processing GML syntax, you can peck at the code in the Ace-based external editor that I made. Processing variable definitions specifically requires you to skip over strings, comments, and loop over values (var name[=value][, name2[=value2]]) with relative degree of confidence (which can be accomplished through a balanced parser).

Is there a way to select code between certain characters

TL DR
Is there some extension, or other functionality, to select code above and below the cursor that is surrounded by some characters, e.g. %% or % ===== or anything else?
Why
I am coding Matlab in VSCode, since the builtin Matlab editor lacks a lot of useful functionalities. However, One thing that I miss is the ability to run 'sections':
%% section 1
x = 0:0.01:pi;
y = sin(y);
plot(x,y);
%% section 2
x = 0:0.01:pi;
y = cos(y);
plot(x,y);
%% section 3
x = 0:0.01:pi;
y = tan(y);
plot(x,y);
A section can be run using Ctrl+Enter. Since I use Matlab on Windows, there is no support for running the Matlab interpreter from the terminal/cmd.
So I created some AutoHotKey scripts that will copy selected code, open the Matlab window, paste it, and run. This works fine, however, when I want to run a very large section, this means that I have to select this manually, and press my defined shortcut to call the AutoHotkey script. Hence above question.
So say I have the cursor at some line insection 2, how can I select only the code in this section (including or excluding section 'headers', these are comments so does not matter).
I have made an extension Select By that allows the selection based on regular expressions relative to the cursor position. You can specify different expressions for forward and backward search and if you want to include the searched text in the selection.
You can set up to 5 regex searches.
The extension defines 5 commands that can be used in keyboard shortcuts
This can (of course) be done by writing a custom extension.
Since this seems like useful functionality in general and an interesting exercise, I've added the required capability to my personal vscode-smcpeak extension.
To use it:
Install vscode-smcpeak. This has to be installed manually for now. At time of writing, 0.0.7 is latest.
Install macros by ctf0. This one can be installed from within VSCode. Restart VSCode after installing it. (See below regarding the choice of "macros" extension.)
Add to your settings.json (File → Preferences → Settings, then click the "Open Settings (JSON)" icon in the upper right):
// Custom macros for ctf0.macros extension
"macros.list": {
// https://stackoverflow.com/questions/57749780/is-there-a-way-to-select-code-between-certain-characters
"selectPercentRegion": [
{
"command": "smcpeak.goToLineMatching",
"args": {
"regex": "^%%",
"moveUp": true,
"select": false,
"allowZeroMove": true
}
},
{
"command": "smcpeak.goToLineMatching",
"args": {
"regex": "^%%",
"moveUp": false,
"select": true,
"allowZeroMove": false
}
},
"smcpeak.revealCurrentSelection"
]
},
Add to keybindings.json (File → Preferences → Keyboard Shortcuts, then click the "Open Keyboard Shortcuts (JSON)" icon in the upper right):
{
"key": "ctrl+alt+m", // or whatever
"command": "macros.selectPercentRegion"
},
(Note: VSCode will always complain that macros.selectPercentRegion is not a valid command. That's a limitation of the "macros" extension.)
Then, with the cursor within a region delimited by the specified regexes, press the keybinding to activate the macro:
Why use two extensions?
The accepted answer, by rioV8 uses a single extension to answer the OP's question. This looks like a good solution to me, and is simpler to use than mine, so I approve of that being the accepted answer.
However, there is an advantage to my approach: it allows the behavior to be modified in settings.json. For example, the OP asked in a follow-up question about having the command additionally copy the text to the clipboard after selecting. Using the macro-based solution, this is an easy modification to settings.json; simply add:
"editor.action.clipboardCopyAction"
to the end of the selectPercentRegion macro. (That is the command normally bound to Ctrl+C.)
Thus, while my suggestion has more moving parts, I personally prefer this approach.
Which macros extension to use?
As Mark noted in a comment, the most popular macros extension, geddski.macros, is unmaintained. It furthermore has a rather serious bug in that the commands are not properly sequenced, leading to race conditions. (I actually saw this once during my testing.)
There are two forks of geddski.macros on the marketplace, l7ssha.macrosRe and ctf0.macros. l7ssha.macrosRe only adds the "delay" ability, which is an awkward way to resolve the race conditions. ctf0.macros, on the other hand, fully fixes the race condition (via a complete rewrite).
Therefore, I recommend ctf0.macros, and in fact have edited my answer to use it rather than the original geddski.macros extension.
However, be aware:
ctf0.macros uses macros.list rather than macros in settings.json
ctf0.macros requires a restart of VSCode after installation to activate
How it works
The additions are in vscode-smcpeak commit 6d5f5d4689.
My extension registers a new command, smcpeak.goToLineMatching, that searches up or down for a line containing a regex. The logic is very simple, just a loop and regex test.
It also registers smcpeak.revealCurrentSelection, needed because otherwise the selected text might be offscreen and nothing scrolls to it by default.
The macro selectPercentRegion uses goToLineMatching to look upwards for one regex, setting the anchor (selection start) there, then uses it again to look down for another regex (although they are the same here), extending the selection to that point. Then it scrolls to ensure the selection is visible.
It should be straightforward to transplant this code to your own extension if you don't want the rest of the stuff that mine provides. (But mine does not bind any keys, so there should be no downside to having the extra stuff.)
Here is another way to do this, with an extension I wrote Find and Transform.
Make this keybinding in your keybindings.json (or it could be a setting too):
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "^(%%[\\s\\S]+?)(?=^$)|(^%%[\\s\\S]+)",
"run": [
"$${",
"const sections = vscode.window.activeTextEditor.selections;",
"await vscode.commands.executeCommand('cursorUndo');",
"const mySection = sections.find(section => section.contains(vscode.window.activeTextEditor.selection));",
"if (mySection) {",
"vscode.window.activeTextEditor.selections = [mySection];"
"}",
"}$$",
],
"isRegex": true,
"postCommands": "editor.action.clipboardCopyAction"
}
}
The find regex finds and selects all %% .... sections. But since you only want the section where your cursor started, some code is run to restore that original cursor and then find that section that contains the one current cursor. Then that section is selected. And in the postCommand that selection is saved to the clipboard.
That is using javascript and the vscode extension api within a keybinding or setting.

Sublime Text 3: Auto-Complete uses incorrect syntax for for loop

With sublime text 3, the autocomplete when typing "for" and hitting tab gives you:
for x in xrange(1,10):
pass
However, this is not a valid statement for python 3. I've tried creating a new build system using the following:
{
"cmd": ["c:/Python37/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
the auto-complete for for still gives the wrong syntax. any advice?
The short version is that the sublime-build and sublime-snippet files that ship with Sublime in support of Python target Python version 2 and not Python version 3. I don't know if that's just due to that being what was used initially or if it's being done on purpose, though.
In Sublime, resources are generally related to a particular language based on the scope provided by the syntax definition. So for example snippets for Python are associated with source.python, your example build file uses that scope to know that it applies to Python files, and so on. As such, no matter what build you happen to be using, that has no effect on the snippets that are being offered.
By way of example, if you use the View Package File command from the command palette and enter the text python for snippet, the list of package resources will filter to Python/Snippets/for.sublime-snippet; pressing Enter to view that resource shows this:
<snippet>
<tabTrigger>for</tabTrigger>
<scope>source.python</scope>
<description>For Loop</description>
<content><![CDATA[
for ${1:x} in ${2:xrange(1,10)}:
${0:pass}
]]></content>
</snippet>
Here the tabTrigger specifies how the snippet inserts, scope controls where it inserts and content controls what it is inserts. Thus, in order to change it to support Python 3, you need to either create your own snippet or modify the existing one.
An issue with creating your own snippet is that it will be added to the list of snippets including the offending one, which allows it to possibly still trigger when you don't expect it to. There is also no general purposes "easy" way to disable individual snippets.
As such, generally the best course of action would be to use the PackageResourceViewer package. Install it, select PackageResourceViewer: Open Resource from the command palette, then select the same file as outlined above and modify the content of the snippet (e.g. replace xrange with range) and save the file.
That will get Sublime to replace the existing snippet with your edited version, so that it takes the place of the existing one and works the way you want.

When folding a line in VS Code is it possible to override the indentation and choose which lines are included in that fold?

Is it possible to customize the way code folding works in Visual Studio Code?
I use a common pattern of defining regions of code across a variety of different document types.
So, for XML I wrap sections of text with <!-- #region --> and <!-- #endregion -->
For c#, I use #region to #endregion,
For TypeScript/Javascript, I use /* #region */ and /* #endregion */.
In full Visual Studio (not VS Code), I have a custom extension which snoops for the pattern across document types, and creates folds based on that, allowing me to create neat, custom document outlines. I'd like to use the same pattern in Visual Studio Code. Is it possible to create a custom VS Code extension which detects these comment patterns, and somehow tags folds based on the patterns?
FoldingRangeProvider can be used if you are looking to contribute custom folding logic in an extension.
Be sure to set your VS Code version in engines in package.json to 1.23, the version that introduced this.
Here's how you'd use one.
export function activate(context: ExtensionContext) {
languages.registerFoldingRangeProvider({ scheme: 'file', language: 'markdown' }, new MyFoldingRangeProvider());
}
class MyFoldingRangeProvider implements FoldingRangeProvider {
provideFoldingRanges(document: TextDocument, context: FoldingContext, token: CancellationToken): FoldingRange[] {
return detectRanges().map(({ lineStart, lineEnd }) => new FoldingRange(lineStart, lineEnd));
}
}
On August 6th 2022 the feature "Fold Selection" was added to "V.S. Code" as part of the sem-minor v1.70.0 release. This new feature gives users complete control over line folds, by total I mean, when & where. Fold Selection allows you to fold whatever code you want, wherever you want.
Below is a GIF image that was appended to the official v1.70.0 release notes
I copy & pasted this image because..,
A. The image shows how the new feature works, and...
B. because it shows that the feature works much like line folding does in IDEs — i.e. VS-22, Intelli-J, CLion, etc...
V.S. Code is actually the first editor I ever used, and I stuck with it for the last 5 years, but one thing I noticed on day 1 of test driving V.S. Code was that it did not have this feature.
Using the new Fold Selection Feature
You can use the feature via the quick input, just type "Fold Selection" until the option pops up for you to select, however, I perfer customizing a keybinding for it.
Here is the default configuration for fold selection in the default keyboard shortcuts JSON document:
{
"key": "ctrl+k ctrl+,",
"command": "editor.createFoldingRangeFromSelection",
"when": "editorTextFocus && foldingEnabled"
}
How to configure the above snippet is beyond the scope of this post, but I suggest keeping the when statement as it is configured above (which is the default).
You can use the keybinding shown in the JSON snippet w/o any configuration, which would be:
CTRL + K CTRL+,
...however, vscode has to attach most all commands to some keyboard shortcut. Most people cannot remember all of the commands and shortcuts, so for features you use often, it makes since to attach it to more practicle option, I like to use something like
CTRL + SHIFT + SPACE SPACE
Its almost like quickly pressing space twice.
Anyways, this is a far better option than what was available before, cheers!
CLICK HERE TO READ THE OFFICIAL RELEASE NOTES
There are three ways to achieve customized folding in a VSCode extension.
You can define regex as folding markers in a [language-name].configuration.json file. (However, we don't have much customization with this approach)
{
"folding": {
"markers": {
"start": "starting regex",
"end": "ending regex"
}
}
}
You can define a FoldingRangeProvider from within the extension as described in this answer. FoldingRange in vscode package supports folding customization with startLine, endLine, and foldingKind.
You can use Language Server support with textDocument/foldingRange. FoldingRange in the vscode-languageserver-protocol supports folding customization with startLine, endLine, startCharacter, endCharacter, and foldingKind.
Check this for more details.
Unfortunately, not at the moment. There is a an open issue in github for this very topic.