VSCode extension development: How can I get the definition file of a type? - visual-studio-code

I am writing a VSCode C++ extension. I would add an include instruction to the current file (which uses the type).
It is like an import instruction in JS.
How can I get the file path of the file where the type is defined?
Is there a way to get the file of goToTypeDefinition or goToDefinition?

you can get the path of the current file with
vscode.window.activeTextEditor.document.uri

Related

How to remove error alert of header-only cmake project in vscode

When I develop a project that output is a shared library or executable, normally, I will use include_directories in the CMakeLists.txt of root directory. In such case, before I include a header within a translate unit, I can't use angle brackets to let headers include each other. I think it is because files in include_directories is not a part of project. I can fix this by using relative path, but when I have to import external project, this can not fixed easily.
Assume that I have a external project with a root CMakeLists.txt, which has this snippet
add_library(mylib INTERFACE)
target_include_directories(mylib INTERFACE .)
and the directory seems like this
└──external
├───mylib.hpp
└───CMakeLists.txt
I want to import this project, and I'm writing a header-only project. The file tree seems like this
├──external (above)
├──include
| ├──header.hpp
| └──CMakeLists.txt
└──CMakeLists.txt
The CMakeLists.txt in root directory is simply add_subdirectory(include), and here is the CMakeLists.txt in folder include
add_library(Header INTERFACE)
target_include_directories(Header INTERFACE .)
target_link_libraries(Header INTERFACE mylib)
Theoretically, I can now include mylib.hpp by #include <mylib.hpp>, but it is not the case. VSCode tells me it can't find this header file. As much as I know, the only way to solve this is add a translate unit and let the implementation code include the header.hpp. I have tested this several time. VSCode won't report this error as long as I add a cpp file and include that header, and once I remove the cpp file, the error appears again.
By the way, I have set "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" in configuration file of vscode.
Please tell me how to fix this without a translate unit.

[Neo]Vim can't open sourced file

So I've got the following init.vim under ~/.config/nvim/
source plugins.vim
plugins.vim lives in the same directory.
When I'm opening [Neo]Vim I always get
Error detected while processing /home/luke/.config/nvim/init.vim:
line 1:
E484: Can't open file plugins.vim
Does anybody know what I am missing?
The path for source command is always taken relative of the current working directory. Hence, you should specify full path instead. For example,
source <sfile>:h/plugins.vim
Another possibility is using runtime command that does searching along the 'runtimepath'. E.g.
runtime plugins.vim

Auto-Format a newly created file using VS Code Extension API

I have created a VS Code Extension here. In this extension, I am creating a linker file that helps me link the local file to the files on the server. When the file gets created it is always created with all content in a single line. How can I ensure that a default formatted is invoked and the contents of the file are not saved all in one line?

How to get the location(path) of the unsaved/untitled new file created in vscode, using node js?

I'm new to creating extension in vscode. I am creating an extension that loads the file using 'fs' and make some changes to the file and write back the file. it works flawlessly for a file that is saved. But it shows:
Error: ENOENT: no such file or directory
for unsaved/untitled file.
I used const loc = vscode.window.activeTextEditor.document.uri.fsPath;
can someone please help me how to get the location of unsaved/untitled file.
An unsaved file does not and cannot have a path. It simply doesn't exist yet in the filesystem. When you create the editor, you will get a reference to the new content and that's it.

How do I make ede-cpp-root-project work with Project.ede

It seems like Project.ede only accepts predefined project, which are Arduino Sketch, Android, Automake and Make. I pasted ede-cpp-root-project, but upon entering project root that has Project.ede, I encountered this error:
eieio-persistent-read: Corrupt object on disk: Unknown saved object
Here is the ede-cpp-root-project I pasted into:
(ede-cpp-root-project "Coloring"
:file "~/workspace/discrete_optimization/hw2/Project.ede"
:include-path '("/"
"/include"
"/include2"
"/include3"))
I really want to create a file per project, not in a centralized file.
Use of a Project.ede file is only for a specific project type of Make or Automake. While you could type it in by hand, you should use ede-new for creating them. Also, only use that type of project if you want EDE to create your Makefiles for you. You are getting the the 'corrupt' message because Emacs will refuse to load the file if it finds anything other than one of the two support project types.
If you would like to use ede-cpp-root-project, you can create any old file like "myproject.el" and put your ede-cpp-root-project config in it. Then do
M-x load-file RET /path/to/myproject.el
to load it up when needed.