VSCode autocomplete/intellisense for strings - visual-studio-code

While working with a PHP or JavaScript file in sublime, if I type the following code:
$test = "Scott";
If($test == null) {
$test2 = "Scott"
}
When I typed in “$test2 = ‘Sc...” Sublime would autocomplete “Scott” since it was a word it found as a string in the current document scope.
However, when I do the same test in VSCode, it doesn’t pick it up or offer any suggestions. Is this possible in VSCode? I have already turned on the quicksuggestions to all true in the preferences. I am not sure what else I could do here or if there is an additional plugin that I need to download. Thanks!

Like this?
My settings:
intelephense extension enabled
{
"editor.quickSuggestions": {
"other": true,
"comments": false,
"strings": true
},
"editor.wordBasedSuggestions": true,
"php.suggest.basic": false
}

These are word based suggestions. They are controlled by the editor.wordBasedSuggestions setting.
However, word base suggestions will only show up when no valid language based suggestions are found (see https://github.com/Microsoft/vscode/issues/21611). JS should do the right thing inside strings here:
But the built-in php language support will still return language based suggestions inside strings, which is why the word based suggestions do not show up

Related

VSCode Code Spell Checker extension - Include TXT files

I'm using the VSCode Code Spell Checker, and I wish to detect typos in TXT files. But the issue is, that I want to detect it on TXT files that are being ignored for search by the VSCode.
Here is my settings.js file:
{
"cSpell.words": [
"BING",
"DOGPILE"
],
"search.exclude": {
"**/.vscode": true,
"**/dist": true,
"**/misc": true,
"**/misc/documents": true,
"**/misc/": true,
"/misc/": true,
"misc/**": true,
"**/misc/**": true,
"**/misc/documents/**": true,
"**/node_modules": true,
"**/sources": true
},
"eslint.validate": [
"javascript"
],
"http.proxy": "",
"http.proxyAuthorization": null,
"http.proxyStrictSSL": false,
"editor.renderWhitespace": "none"
}
The file that I want to detect typo located inside the misc directory, that is declared in the "search.exclude" list (I don't want search results from any files in this directory), but I DO WANT to detect typos on theses files.
If I remove all of theses and the settings.js looks like that:
{
"cSpell.words": [
"BING",
"DOGPILE"
],
"search.exclude": { },
"eslint.validate": [
"javascript"
],
"http.proxy": "",
"http.proxyAuthorization": null,
"http.proxyStrictSSL": false,
"editor.renderWhitespace": "none"
}
The TXT file typo detection works, but I get the search results that include files inside the misc directory, which is NOT what I want.
Has anyone faced this issue before and know how to solve it?
Thanks.
as it is explicitly stated in Spell Checker FAQ
What files are excluded by the spell checker?
By default the spell checker excludes the same files excluded by the VS Code search.exclude setting. See discussion: #16, #55 and #95
the correct way to address the problem exposed in the question I would say is to open an issue to the project for a new feature request to allow to override this default behavoir.
as a last resort if you absolutely need to prevent search.exclude from being added to the exclusions you could try to force the code installed on your machine and after restarting vscode you will have the spell check working also in those paths.
DISCLAIMER please note what is illustrated below is a discouraged hack mentioned in here for informational purposes only, obviously if you try this kind of workaround you must be extremely aware of what you are doing
for example:
assuming the extension was installed in ~/.vscode/extensions/streetsidesoftware.code-spell-checker-1.10.2
in the source server/config/documentSettings.js there is the function async fetchSettingsFromVSCode(uri) where it is added :
ignorePaths: ignorePaths.concat(CSpell.ExclusionHelper.extractGlobsFromExcludeFilesGlobMap(exclude)),
by changing in to
ignorePaths: ignorePaths
then quit fron vscode and when re-open it the extension no longer excludes the same files excluded by the VS Code search.exclude setting.

Visual Studio Code: How to Disable `editor.codeActionsOnSave`

I find that the editor.codeActionsOnSave configuration very annoying because a lot of times when I save, it eats up some time getting some code actions for the language I'm currently using (Golang, for example).
I have no desire nor need for such a feature as I have configured my desired actions upon save elsewhere.
I have already added the following entries on my VS Code configuration file:
"editor.codeActionsOnSave": {},
"[go]": {
"editor.codeActionsOnSave": {}
},
However, setting an empty value does not seem to disable this annoying feature because I still get popups that VS Code is getting some code actions.
Previously, editor.codeActionsOnSave was set to null by default, but this also does not disable the feature.
How do I disable this feature?
EDIT: I also filed a GitHub issue about this here.
You can delete it or set it to false
...
"editor.codeActionsOnSave": {
"source.fixAll": false,
"source.organizeImports": false
},
"editor.formatOnSave": false
...
I managed to get rid of it by setting it only for go :
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
}
}
Setting it to null does not work, somehow Go expects it to be set up, and setting it to false globally can be a pain on other projets (I am looking at you react-native project with eslint set up)

How can I configure multiple formatters to run in a sequence on save in VSCode?

I am working on a Haskell project that must be formatted by both:
stylish-haskell (for import reordering)
brittany (for general formatting)
I can set the single default formatter for a language:
"[haskell]": {
"editor.defaultFormatter": "MaxGabriel.brittany"
}
or I can select one from a list using editor.action.formatDocument.multiple ("Format Document With... in the command palette).
But I need to run both of them, one after the other, on save. As of right now, I can only run the single default formatter on save. The order doesn't matter in this case, but it could in more general cases.
I've tried setting editor.defaultFormatter to a list of formatters (this didn't work, as expected) and built a local extension that calls editor.action.formatDocument.multiple with various arguments, which just brings up a drop-down list of available formatters to choose from.
How can I run both formatters sequentially on save?
I don't think this is really a use case that is officially supported, but you could possibly work around it by having an extension do the following:
disable "editor.formatOnSave" for Haskell
register a callback for vscode.workspace.onDidSaveTextDocument, in which you:
set "editor.defaultFormatter" to the first formatter using the WorkspaceConfiguration API
call "editor.action.formatDocument"
set "editor.defaultFormatter" to the second formatter
call "editor.action.formatDocument" again
Of course, this only covers formatOnSave formatting, not formatOnPaste or formatOnType.
It's a bit late but, for newcomers, you can also use one extension that is already created... and it's thanks to all the answers of this post, by the way.
See Multi Formatter
So you can just add the formatters you want to run in the settings.json or the *.code-workspace settings like this:
{
"[haskell]": {
"editor.defaultFormatter": "Jota0222.multi-formatter",
"editor.formatOnSave": true
"multiFormatter.formatterList": [
"vigoo.stylish-haskell",
"MaxGabriel.brittany"
],
}
}
With that configuration, stylish-haskell will run first and Britanny will run just after once you save the changes.
P.S.: I'm indeed the author of the solution. I'm not aiming to do any promotion, it's just an implementation of the answers above. So I'll like to thank the people that answered before me.
Also, the extension is open sourced, feel free to check the code and contribute on GitHub
Last thoughts: look at Run on Save extension and execute your formatters not as extensions but as scripts.
Previous edit:
If your formatter doesn't contribute a command (see discussion in comments for some that do) as it appears brittany does not, try something like this for its task:
{
"label": "brittany format step",
"type": "shell",
"command": "brittany ${file}",
"problemMatcher": []
}
From Gama11's answer of creating a VSCode extension:
The following code specifies the formatter and then formats the code.
const config = vscode.workspace.getConfiguration('editor', vscode.window.activeTextEditor?.document);
await config.update('defaultFormatter', 'MaxGabriel.brittany');
await vscode.commands.executeCommand('editor.action.formatDocument');
Therefore, the answer is:
const config = vscode.workspace.getConfiguration('editor', vscode.window.activeTextEditor?.document);
const formatters = ['MaxGabriel.brittany', 'ms-python.python'];
formatters.forEach(async formatter => {
await config.update('defaultFormatter', formatter);
await vscode.commands.executeCommand('editor.action.formatDocument');
});

VSCode extension: disable repeating things in code completion

I am creating a language extension for VSCode using Java and the LSP4J library. It is something like this.
But I have a problem - if the user presses Ctrl+Space, and the language server returns an empty list, VSCode will still offers its options - things that are already in the code. How can I get it to display something like "No suggestions" instead?
The text-based completion you're seeing there can be disabled with the "editor.wordBasedSuggestions" setting.
Extensions can change the default value of a setting for a particular language by contributing configurationDefaults in package.json:
"contributes": {
"configurationDefaults": {
"[lang]": {
"editor.wordBasedSuggestions": false
}
}
}
Where lang is the ID of the language in question.
If the language server sends back an empty list you could add an artificial entry with text: "No suggestions" to the completion list.

Set language of a text document in a VSCode extension

I have a Visual Studio Code extension where I try to open a
virtual editor:
vscode.workspace.openTextDocument(vscode.Uri.parse(previewSchema + ":" + path))
context.subscriptions.push(extractHibernateLogCommand, vscode.Disposable.from(
vscode.workspace.registerTextDocumentContentProvider(previewSchema, hibernateExtractorProvider)
));
Those documents are always language:plain-text. Is it possible to change this programmatically to "SQL" to have the correct highlighting?
Full code
Since VSCode 1.28 (September 2018), it's also possible to set the language mode for a document after it has been created using languages.setTextDocumentLanguage():
Set (and change) the language that is associated with the given document.
Note that calling this function will trigger the onDidCloseTextDocument event followed by the onDidOpenTextDocument event.
Here's a simple example that opens a document containing {} and sets the language to JSON:
vscode.workspace.openTextDocument({content: "{}"}).then(document => {
vscode.window.showTextDocument(document);
vscode.languages.setTextDocumentLanguage(document, "json");
});
I found a solution by myself:
let options: Object = {
content: string,
language: "sql"
};
vscode.workspace.openTextDocument(options).then(doc => {
vscode.window.showTextDocument(doc, vscode.ViewColumn.One);
}, err => {
vscode.window.showErrorMessage(err);
});
A solution when using TextDocumentContentProvider seems not to be possible.
The commit with my change
Open the Command Palette (View->Command Palette)
run "Configure language specific settings"
In the Select Language drop down there should be a setting for SQL