Opening files referenced with relative pathnames in Visual Studio Code - visual-studio-code

Often, I am editing files that reference other files, and I would like to be able to Cmd-click the filenames to open the referenced file.
An example would be a configuration file that points to another config file, cf. the following JSON snippet:
"structureDefinitionFilename": {
"doc": "Filename of the Structure definitions",
"default": "./structureDefinition.json"
},
When my cursor is on the ./structureDefinition.json, I would like to be able to run a command that opens that file. (I am not using the mouse a lot, so I was just mentioning Cmd-click as an example.)
Another example would be Python source code which opens a data file:
with open('data/structures.lst') as f:
...
Again, it would be nice to be able to quickly open data/structures.lst.
Is there any built-in method or an extension that allows this?

Related

VS Code Go to Implementations / Definition (aka F12, aka CTRL+click) doesn't work

Normally when I choose either "Go to Definition" or "Go to Implementations" from the context menu on a function name of a function definition in a JavaScript file, or press F12 or CTRL+Click on the function name, VS Code shows in a popup all the places across the project where that function is used.
For some reason this doesn't work with some of my projects.
Here is a simple repro structure that still has the problem for me:
foo.js
bar
|_ bar.js
// foo.js
import bar from './bar/bar';
export const foo = () => {
bar();
};
// bar/bar.js
export default function bar() {
console.log('function bar called')
}
That's all.
The command works fine when both files are open in the editor. The command also works if both files are in the same directory, no matter root or deeper. If I rename or move one of the files, the changes get correctly automatically reflected in the import line of the other file, which means VS Code understands the file structure.
Why doesn't the definitions / implementations popup appear, neither for .js files, nor for .ts files?
You must include a jsconfig.json or a tsconfig.json file in the root of your project in order for the InteliSense to understand that you are indeed inside a JavaScript project and to link the files together rather than treating them as separate entities. See details on jsconfig.json in VS Code documentation.

Browsing lines/columns in a big file in VSCode

I work with big flat database files which I like to inspect at line/column.
I have a tool which generates a log with hundreds of lines like:
code --goto path/to/my/file:line:column
Visiting one such a spot works ok if I throw one such a line into the command line window and click it from there, but not from the editor (at least I have not found the way)
I would like to be able to have this log file open in the VSCode editor and hover/click in every line which would take me to the spot I want to visit in the big db file. Is this possible at all?
Should I convert my log file to some script format which supports the hover and click functionality?
Is there otherwise any extension available which can do the trick here?
Thanks a lot.
Miguel
Edit
Using HTML Related Links v0.9.0
I guess we're almost there but it's still not working for me.
The concrete scenario I have is: I have a file
d:\Users\Abc.def\Git\lingware-data\test\spx\spm\spm_cfg3\spm_cfg3_tst_row_valid_lng_abbr.dbr
containing several lines of the type
code --goto d:\Users\Abc.def\Git\lingware-data\dct\spx\dct.u08:176:36
(probably code is not needed, I tried with and without it)
In my settings.json file:
"files.associations": { ".u08": "csv (pipe)", ".dbr": "html" },
"editor.largeFileOptimizations": false,
"html-related-links.include": {
"html": [
{ "find": "--goto ([a-z:]+[^:]+):(\\d+):(\\d+)",
"filePath": "$1",
"lineNr": "$2",
"charPos": "$3"
}
]
}
if I use "plaintext" as languageId I don't get the HTML related links section displayed in the Explorer area, so I am using "html" - I have updated to v0.9.0 of the extension, and what I continue seeing in the explorer view is:
"d:\Users\Abc.def\Git\lingware-data\dct\spx\dct.u08 d:\Users\Abc.def\Git\lingware-data\test\spx\spm\spm_cfg3\d:\Users\Abc.def\Git\lingware-data\dct\spx\dct.u08"
If I click on it nothing happens I hope this is more or less clear.
What am I doing wrong?
*** edit ***
Installed v0.9.1. Enabled Developer console to debug click behavior on a row. What get now is:
[Extension Host] Clicked on: d:\Users\Miguel.Duran\Git\lingware-data\dct\spx\dct.u08
console.ts:137 [Extension Host] goto: 175:28
You can use the extension HTML Related Links v0.9.0 (it not only applies to HTML)
Define this in settings.json
"html-related-links.include": {
"plaintext": [
{ "find": "--goto ([^:]+):(\\d+):(\\d+)",
"filePath": "$1",
"isAbsolutePath": true,
"lineNr": "$2",
"charPos": "$3"
}
]
},
"html-related-links.removePathFromLabel": true
After loading the log file you can lock the Explorer View HTML RELATED LINKS to this file. Use lock button in top right of the view. Now only changes to this log file will update the view.
In an absolute path the label will contain the path and the view adds the directory path at the end. You can remove the path from the label with the setting html-related-links.removePathFromLabel
Click on a row in the view and that file will be opened to the given line:char position.
If on Windows the path is absolute you have to specify the drive first because it contains a :
"find": "--goto ([a-z]:[^:]+):(\\d+):(\\d+)"
Edit
There was a small typo in the regular expression string (need escaping the escape char).
Add possibility for absolute paths.
I would like to be able to have this log file open in the VSCode editor and hover/click in every line which would take me to the spot I want to visit in the big db file.
Is this possible at all?
If you visualize your file in a VSCode terminal, VSCode 1.70 (July 2022) will offer a way.
See issue 140780 ("Jump to line+column in terminal links (format <path> l:c)"), PR 153957 ("Detect terminal links with space, then line:col") and PR 155904 ("Ensure folders with spaces don't impact link line/col")
To test, you can open a terminal and run echo ./myFile.txt 10:5 (replacing the filename with one that exists).
When you click to open the file, it should open to line 10, column 5.
This is available today (July 2022) in Code insiders.

How to set default file type to be All Files(*.*) in VS Code?

Now my default file type of Visual Studio Code is Plain Text.
If I save a new file with name like a.in, it will save as a.in.txt. I have to change the dropdown to All Files.
I find the same problems in github: 1, 2. However, they seem not solve my problem.
This is not possible in the general case in VSCode.
The issue is that you cannot assign "no extension" to a language, and as per the links you mentioned, the All Files (*.*) option is disabled by upstream (electron).
Therefore, you will either have to remove the extension manually, OR you can create the file first (using the terminal, Explorer, an extension, etc.) and then open that existing file.
There is a way to change the default extension (but not to All Files)
Add the following line to your settings.json
"files.defaultLanguage": "<language>",
Replace <language> with the language of your choice.
Now, whenever you make a new a file, the default file language will be <language>.
A special value for <language> is ${activeEditorLanguage} which is the language of the file last opened (useful if, say, you copy a piece of code from one file to save as another).
Unfortunately, this does not fully answer the question, but provides a partial solution.

Is there a way to tell VS Code that the firstLine match in my language extension should overrule any file extension?

I am currently developing a language extension for VS Code. The language I am developing it for usually comes with either its own file extension (let's call it .myext) or simply .txt (it's just a scripting language).
I have run into the issue that I am not sure how to tell VS Code to use my extension when the first line matches a specific string when opening .txt files.
This is what I am trying (excerpt from my package.json):
"contributes": {
"languages": [
{
"id": "mylang",
"aliases": [
"mylang"
],
"extensions": [
".myext"
],
"firstLine": "^MyLang.*",
"configuration": "./language-configuration.json"
}
]
}
This does not work when I open a .txt file that starts with MyLang. It does however work if I open a file that has an unknown (e.g., file.foobar1234) or no extension at all.
When I now change the configuration to include .txt files, it will activate my extension for any .txt file I open:
"contributes": {
"languages": [
{
"id": "mylang",
"aliases": [
"mylang"
],
"extensions": [
".myext",
".txt"
],
"firstLine": "^MyLang.*",
"configuration": "./language-configuration.json"
}
]
}
However, I do not want this, I want to keep the default plain text setting when opening normal .txt files.
In short, what I want to achieve:
have my extension activate on my custom file extension .myext (this already works without any issues)
have my extension activate when opening .txt files that start with the string specified via firstLine
do not have my extension activate when opening .txt files that do not start with the string specified via firstLine
Is there a way to do this?
After some extensive search: this is currently not possible.
There was a recent change (mid-late 2017) that allows language extensions that match the file extension and the first line of a file to get precedence over an extension that just matches one of the two.
This does, however, not work with the built-in language modes of VS Code. Matching a file extension will make it so VS Code always chooses your language extension for such files if there if there is no other extension available for that language, regardless of any first line rule that might have been set in addition.
See here and here for additional information.
If you have the latest version of VS Code, I say this because it's what I have might be probably is supported in earlier versions. On the bottom toolbar you should see the language that is auto-detected for the file you have open. For example, if you have a .js file it will auto-config for JavaScript. Whatever language defaults for you file, just click on it and it will bring you into 'Select Language Mode'.
From there you'll have options. You can configure/customize what happens and/or Search for an extension to fulfill the Language mode. So, I think you'd want to do the latter especially if you're creating your own language file.

setting default syntax for sublime doesn't work

Whenever I open a .m file the default syntax is objective-c but I want to change that to matlab. So when I open a .m file I go to
view -> syntax -> open all with current extension as..
and choose matlab. After that if I open another .m file the default syntax is matlab. But if I close sublime and open it again the default syntax is back to objective-c.
I added objective-c to ignore syntax list but now it opens .m files as plain text. Is there another way to set default syntax for a file extension?
In sublime3, view -> syntax -> open all with current extension as.. works if you close then open sublime.
I'm not sure why Sublime is doing this, but there's a way around it. Since you're using ST2, it's quite easy. Select Preferences -> Browse Packages... to open your Packages folder, and open the Objective-C subfolder. Open Objective-C.tmLanguage in Sublime as an XML file, and comment out (or remove entirely) Line 7:
<string>m</string>
Save the file and close it. Next, open Objective-C++.tmLanguage and do the same thing to Line 8:
<string>M</string>
Save and close that file as well. Finally, just to make sure your Open all with current extension as... command worked, go to Packages/User and check for the existence of a file called MATLAB.sublime-settings. Open it as a JSON file, and double-check that its contents are as follows:
{
"extensions":
[
"m"
]
}
If for some reason the file doesn't exist, create it with the above contents and save it. You should now be able to open any .m file and have the syntax automatically set to MATLAB.
Note for Sublime Text 3 users:
ST3 does not store its default packages in Packages anymore, instead they're in the Installed Packages folder (its location varies by OS and type of install) as zipped .sublime-package files. To access the contents, install #skuroda's PackageResourceViewer plugin to seamlessly access the contents of these files. Once installed, open the Command Palette (CtrlShiftP on Windows/Linux, ⌘ShiftP on OS X) and type prv to bring up the PackageResourceViewer options. Select Extract Package, scroll down to Objective-C, and hit Enter. You can now follow the directions above for opening the Packages/Objective-C folder and editing the .tmLanguage files. The Packages/User directory is a regular directory, so you don't need to extract it to verify the contents of MATLAB.sublime-settings.
I think why this is happening, at least in Sublime 3 (v3.2.1). After using PackageResourceViewer: Extract Package to extract Matlab package, I found that in Matlab.sublime-syntax in Line 6:
- matlab
As .matlab is not an extension for matlab functions and scripts, after I changed it to - m, Sublime shows .m files in MATLAB syntax.