Disable peek in Visual Studio Code - visual-studio-code

Is there a way to disable the ctrl-click 'peek' feature in Visual Studio Code? Ideally I'd like ctrl-click to just open the file containing the definition in a new tab.
Edit: I submitted an issue to at least make it less confusing. Apparently my terminology is slightly wrong.
To clarify, there are two actions:
Right-click -> Peek Definition
Right-click -> Go to Definition (bound to ctrl-click)
Their behaviour is as follows:
PD, Single Definition
Opens inline interface showing definition.
PD, Multiple Definitions
Opens inline interface showing definitions.
GtD, Single Definition
Open the file containing the definition.
GtD, Multiple Definitions
Pick one of the definitions at random, open that file, and an inline interface showing all the definitions.
All of those are fine except the last. Doing both things results in a really redundant and confusing UI like this:
There should be a way to have one of these behaviours:
Pick one of the definitions at random, open that file.
Or:
Open inline interface showing all the definitions (in the current file)

I've made a pull request to fix this https://github.com/Microsoft/vscode/pull/68023, but until then here's a temp fix that patches the VSCode installation files. You'll need to re-apply every update.
EDIT: The fix was merged into vscode. It should be in later versions.
With this fix Ctrl+Click will:
Use peek if there are multiple definitions
When using peek, will not navigate to the best match in the editor and cause you to lose your spot
If there is only one definition, it will navigate to the best match and NOT open peek.
Figure out what the function that needs to be patched looks like. The method is DefinitionAction.prototype._onResult(editorService, editor, model)
https://github.com/Microsoft/vscode/blob/e82d8bb6e6c8fd07ca16eacd16663ebd221187cb/src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts#L128
Go to the VSCode installation directory. %LocalAppData%\Programs\Microsoft VS Code and right click and open the directory in VSCode so that we can use VSCode's search feature to search for text in every file.
Search for _onResult and evaluate every result, checking to see if the signature and body matches what we are expecting from the function we saw in step 1.
We know from step 1, the function _openReference is nearby. Use that to narrow the search.
I found it in workbench.main.js line 2454. Use bracket matching to find the end or know that it ends immediately before t.prototype._openReference
The function when formatted is the following (async func is compiled down to statemachine, that's why it looks nothing like the source typescript):
t.prototype._onResult = function (e, t, r) {
return i(this, void 0, void 0, function () {
var i, s, a;
return n(this, function (n) {
switch (n.label) {
case 0:
return i = r.getAriaMessage(), o.alert(i), this._configuration.openInPeek ? (this._openInPeek(e, t, r), [3, 3]) : [3, 1];
case 1:
return s = r.nearestReference(t.getModel().uri, t.getPosition()), [4, this._openReference(t, e, s, this._configuration.openToSide)];
case 2:
(a = n.sent()) && r.references.length > 1 ? this._openInPeek(e, a, r) : r.dispose(), n.label = 3;
case 3:
return [2]
}
})
})
}
Replace the function with the following (if using same version) or format and edit the function you found to be similar to this example. Note the o variable is the global\window object and subject to change.
t.prototype._onResult = function (e, t, r) {
return i(this, void 0, void 0, function () {
return n(this, function (n) {
switch (n.label) {
case 0:
return r.getAriaMessage(), o.alert(r.getAriaMessage()), this._configuration.openInPeek || r.references.length > 1 ? (this._openInPeek(e, t, r), [3, 3]) : [3, 1];
case 1:
return [4, this._openReference(t, e, r.nearestReference(t.getModel().uri, t.getPosition()), this._configuration.openToSide)];
case 2:
r.dispose(), n.label = 3;
case 3:
return [2]
}
})
})
}
Launch VSCode. You will get a Your Code installation appears to be corrupt. Please reinstall. Just hit the gear icon and click Don't Show Again.

I tried to find a workaround changing the behavior of CMD + Click to go to implementation but it appears there is no solution yet?
The VSCode documentation shows its set by default to go to definition without a way to modify it:
https://code.visualstudio.com/docs/editor/editingevolved
On my machine (Mac) if I press CMD + Click or F12 on a method it will direct me to the Peek view on the definition, however CMD+F12 will direct me to the implementation without the peek appearing.

This seems to have been fixed in a newer version. If I now hover over FOO in foo.cpp, I see the normal tooltip #define FOO 2. If I press Ctrl, the message expands to add the text "Click to show 2 definitions" and if I click while still holding Ctrl, I get the peek window, as requested.

Related

CTRL + hover doesn't show definition for my vscode extension?

I write a VSCode extension to support a new language. It uses registerDefinitionProvider() to register a definition provider. And it works when pressing F12, ctrl + click the symbol, or right-clicks and choosing the "Go to Definition".
But VSCode shows nothing when I use ctrl + hover on the symbol since VSCode 1.67. It works well before 1.67.
It shows nothing since VSCode 1.67
It works well if the VSCode version lower than 1.67
The ts code:
context.subscriptions.push(vscode.languages.registerDefinitionProvider(['test'], {provideDefinition(doc, position, token) {
var word = doc.getText(doc.getWordRangeAtPosition(position));
var rst:vscode.Location[]|undefined = macroManager.getPositionByMacro(word);
return rst;
}}));
I know how to fix this.
constructor Location(uri: Uri, rangeOrPosition: vscode.Range | vscode.Position): vscode.Location
The second parameter of the Location constructor can be Range or Position. Ctrl+ hover shows nothing if it's a Position.
So the code should be:
let range_begin = new vscode.Position(lineNum, 0);
let range_end = new vscode.Position(endLineNum, 0);
let position_range = new vscode.Range(range_begin, range_end);
positionLink.push(new vscode.Location(value.resourceUri, position_range));
check your extensions. it happens when the VS Code built in extensions are disabled.
search #builtin in the extensions search bar and make sure every extension is enabled

How to trigger activation of the vscode markdown extension

In my VS Code extension I have some code that uses the built in Markdown extension. I capture a reference to it by registering as a markdown plugin and putting the following code at the end of my extension's activate method.
return {
extendMarkdownIt(mdparam: any) {
return md = mdparam;
}
};
Markdown calls this when it activates.
Generally this is not a problem. Most of the use cases for my extension involve a markdown file already loaded into the active editor, and the loading of this file triggers activation of the markdown extension.
However there are some legitimate use cases in which this is not so.
I need to programmatically trigger activation of the markdown extension. Some of these cases involve having a different kind of file open in the active editor so loading a markdown file into it is not an acceptable option.
Some potential strategies:
Change the language mode. There is a command workbench.action.editor.changeLanguageMode but no documentation. I tried
vscode.commands.executeCommand('workbench.action.editor.changeLanguageMode', 'md');
but this triggers the UI
so I tried a pattern I've seen in the parameters of other commands and added , true. This suppressed the UI but doesn't seem to work.
Load a markdown file into a new editor then close it again. This should work, but it's ugly.
Put something in the contributions section of my extension that changes the activation trigger for the markdown extension so that it is triggered by the other file types on which my extension operates.
Of these options my favourite would be 3 but I don't even know whether this is even possible. Option 1 is hampered by the crappy (in many cases non-existent) documentation for vscode internal commands.
Option 1 it is. If anyone knows how to do option 3 please tell, the solution below is a ghastly hack.
It is possible to trigger activation of the Markdown extension by changing the document language of any open editor to markdown. In the event that there are no open editors a document with the markdown language set can be created in memory and loaded into an editor.
If VS Code is busy loading extensions activation can take several hundred milliseconds so the best thing to do is watch the variable into which markdown-it is captured.
The variable md is a global (global to my extension, not the whole of VS Code) into which a reference is acquired as shown in the question.
let ed = vscode.window.activeTextEditor;
if (ed) {
let lid = ed.document.languageId;
if (lid !== "markdown") {
vscode.languages.setTextDocumentLanguage(ed.document, "markdown").then(
function waitForMd() {
if (md) {
vscode.languages.setTextDocumentLanguage(ed!.document, lid);
} else {
setTimeout(waitForMd, 100);
}
}
);
}
} else {
vscode.workspace.openTextDocument({ language: "markdown" }).then(doc => {
vscode.window.showTextDocument(doc).then(
function waitForMd() {
if (md) {
vscode.commands.executeCommand("workbench.action.closeActiveEditor");
} else {
setTimeout(waitForMd, 100);
}
});
});
}
Once the capture completes we can restore the true language or close the editor as appropriate. To be realistic the second case (no active editor) is unlikely because my own extension won't activate until you load something. At any rate it works stably now. The larger project is progressing nicely.

Using Chrome Dev Tools Code Folding Shortcuts

On the Settings > Preferences > Source we can enable Code Folding on the Chrome Dev. But I didn't find a way to use keyboard shortcuts to, eg. collapse all, etc. Did look for in the Shortcuts section window and in the Full Listings also, but no success. I'm assuming we can't do it on the current version. In case someone knows about it, I'll be very happy to know it too.
There's no such hotkey so try suggesting this feature on https://crbug.com.
Meanwhile you can add it manually:
make sure "code folding" is enabled in devtools settings (in the "Sources" group)
run this code in devtools-on-devtools (see the instruction below)
[
['Shift-Ctrl-[', 'fold'],
['Shift-Ctrl-]', 'unfold'],
['Shift-Ctrl--', 'foldAll'],
['Shift-Ctrl-=', 'unfoldAll'],
].forEach(([key, cmd]) => {
CodeMirror.keyMap['devtools-common'][key] = CodeMirror.commands[cmd];
});
close devtools-on-devtools
This will last only for the current devtools instance.
For convenience you can save the code in snippets and run it later from there or by typing the snippet name in the commands palette (Ctrl-P or Cmd-P hotkey).
How to open devtools-on-devtools:
Open devtools first and switch its Dock side in the menu to a detached (floating) window
in the now detached devtools press CtrlShifti or ⌘⌥i on MacOS,
which will open devtools-on-devtools in a new window
The top answer is great, but chrome devtools has been updated since to use CodeMirror 6, which requires a codeMirror instance to be passed to the functions:
CodeMirror.commands.foldAll();
Uncaught TypeError: Cannot read properties of undefined (reading 'operation')
at CodeMirror.commands.foldAll (foldcode.js:105:8)
at <anonymous>:1:21
line 104-109 from foldcode.js:
CodeMirror.commands.foldAll = function(cm) {
cm.operation(function() {
for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
cm.foldCode(CodeMirror.Pos(i, 0), null, "fold");
});
};
I could solve it by debugging a codemirror event to save the cm instance as a global variable (temp1), but I would appreciate if someone could have a better solution to this that can be automated.
Edit: I found the codeMirror instance at document.getElementsByClassName("CodeMirror")[1].CodeMirror, so a working solution is:
CodeMirror.commands.foldAll(document.getElementsByClassName("CodeMirror")[1].CodeMirror)
the shortcut setting still doesn't seem to work though.
(with
CodeMirror.keyMap["devtools-common"]["Ctrl-k"]=function()
{let {CodeMirror:instance}=
document.getElementsByClassName("CodeMirror")[1];
CodeMirror.commands.foldAll(instance);
};
)
Edit 2: oh, it just needed capital K, so "Ctrl-K". Now the shortcut works too. hurrah
btw i've commented on the folding feature's thread on chromium bugtracker to include this, because it is incredibly useful for skimming files while editing/reading. No response yet, idk if anyone's even paying attention to comments there anymore:
https://bugs.chromium.org/p/chromium/issues/detail?id=328431
CodeMirror 6 supports the folding shortcuts natively (per foldKeymap):
Ctrl-Shift-[ (Cmd-Alt-[ on macOS): Fold the lines that are selected, if possible.
Ctrl-Shift-] (Cmd-Alt-] on macOS): Unfold folded ranges on selected lines.
Ctrl-Alt-[: Fold all top-level foldable ranges.
Ctrl-Alt-]: Unfold all folded code.

How to clear method call parameters/arguments filled by SublimeText Jedi-autocomplete?

I am testing out SublimeText auto-complete using JEDI package and one problem I am having is unrequired parameters are auto-filling function/method calls:
For example in Flask:
I can just call the function as such:
app.run(),
but JEDI-Autocomplete is doing something like this:
app.run(host= , port=..., debug=..., load_dotenv=...)
I can't figure out how to clear the parameters as it's not needed in this case.
Same problem with:
app = Flask(__name__)
Instead autocomplete is automatically filling in unrequired parameters and seemingly forcing me to add value to each argument.
Searching the Sublime Text 3's SublimeJEDI repo (issue #290 - open() autocompletes all args when in required mode) seems to suggests that there's an option to control autocomplete aggressiveness:
"auto_complete_function_params": "all",
In your preference settings (either user preferences or syntax preferences).
From their README.md:
Function parameters completion has 3 different behaviors:
Insert all function arguments on autocomplete:
# complete result
func(a, b, c, d=True, e=1, f=None)
# sublime_jedi.sublime-settings
{
"auto_complete_function_params": "all"
}
Insert only required arguments that don't have default value (default behavior):
# complete result
func(a, b, c)
# sublime_jedi.sublime-settings
{
"auto_complete_function_params": "required"
}
Do not insert any arguments:
# complete result
func()
# sublime_jedi.sublime-settings
{
"auto_complete_function_params": ""
}
More info about auto_complete_function_params
You may experiment with those options to see what fits you better.
Turning auto_complete off and using SublimeJedi: ShowDocstring (defaults to Ctrl+Alt+D) when the cursor is after the function name fixes the issue for me.
You can also hover over the function name or use SublimeJedi: Show Signature.

Can the Eclipse code style formatter align assignment statements?

Eclipse has an option in Indentation: "Alignment of fields in class declarations" that will
align assignments of class members as in:
class Foo {
int x = 3;
String y = "abc"
long z = 2;
}
Is there a setting that will do the same for method level assignments as in:
private void foo() {
int x = 3;
String y = "abc"
long z = 2;
}
I can not find such a setting.
Try creating a new Java Code Style Formatter profile (Window->Prefernces->Java->Code Style->Formatter) or modifying an older one and check Indentation and Braces tabs.
For future visitors.
There is no such option in Eclipse, but, as mentioned here, you can use plugin named OCDFormat that aligns assignments and declarations in a selected piece of code.
Installation: download the latest version of OCDFormat_*.jar from project page on github (click → Raw → Save as) and add it to your Eclipse plugins or dropins directory.
Usage: select a piece of code and press Ctrl+4.
The Eclipse code style formatter cannot align assignment statements.
columns4eclipse is plug-in that allows you to do so. I had made a gif video illustrating its use but my answer got deleted by a moderator (as >10k users will see), so I let you try the plug-in and see by yourself.