0

I'm trying to make more daily use of Visual Studio Code, but there's one feature I really miss from my existing GVim setup: I use syntastic to compile C++ source when I save the current file, and then I can jump directly to error lines in the editor.

Is there any way to set up VS Code to do something similar?

I know that I can create a tasks.json and have Ctrl+Shift+B compile the whole project, but the turnaround for that is excessive. I just want to compile the current file when saving it, and have the error list be navigable (presumably in the Problems tab).

Success criteria:

  • When I save a C++ file, it runs a configured g++ command, rather than build the entire project.
  • It should somehow parse the output from that, and make the warnings and errors easily navigable so that I can step through them with a simple keystroke. Navigation should place my cursor on the relevant line. In Visual Studio, this used to be bound to the F8 key.
ifconfig
  • 6,242
  • 7
  • 41
  • 65
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380

1 Answers1

0

VS Code is not an IDE, it is a text editor with extension support, and it cannot be treated as such. Natively, it does not have any features allowing it to parse error output from a build task and visualize it in the editor.

The linter has the ability to markup the editor when syntax errors are present, but that is all the stock linter is capable of doing. It is the only part of VS Code that has the ability to use the Problems panel and annotate the editor with syntax errors.

That said, you are able to accomplish the 1st point you require. You can set up a keybind that executes more than one command. In your case, a keybind that would save the file and then build it. Something like:

{
    "key": "ctrl+shift+b",
    "command": "workbench.action.files.save",
    "command": "workbench.action.tasks.build"
}

in keybindings.json would save the file and then execute the default build task when you press Ctrl+Shift+B.

ifconfig
  • 6,242
  • 7
  • 41
  • 65
  • Given that this isn't necessarily valid JSON (two keys with the same name is implementation-defined), I'm not convinced it'll work. Have you successfully done this? – Roger Lipscombe Oct 03 '17 at 08:41
  • Fair enough; it still doesn't answer all of my question, though. The parts missing: how do I run a shell command on the _current_ file from there? How do I get the results, parsed, into (e.g.) the Problems tab? Do I need to write my own plugin? – Roger Lipscombe Oct 03 '17 at 14:47
  • If you have the command you need to get run in a build task, you can point the `command` of the keybind to execute the default one as shown above. The output of the build task will appear in the *Terminal* tab. – ifconfig Oct 03 '17 at 15:04
  • using the `#relativeFile` keyword, you can have the build task compile the currently in-focus editor. *See: https://stackoverflow.com/questions/45679035/build-currently-opened-file-in-visual-studio-code?pagesize=50*. – ifconfig Oct 03 '17 at 17:55
  • ...and how do I get the errors and warnings in such a form that I can easily navigate from one to the other? – Roger Lipscombe Oct 03 '17 at 18:18
  • It is unclear to me what exactly you are requesting with that. The `Problems` tab is exclusively for use by the linter. Errors and warnings will show up in the `Terminal` tab. What do you mean by *navigate from one to the other*? From what to what? – ifconfig Oct 03 '17 at 18:21