Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code API - remote calls

I am currently investigating VS code extensions in conjunction with a code generation project. While I understand that the VS code API is fairly full-fledged to allow in-extension manipulation of documents I can't figure out how to do this outside of the extension.

Are there ways of remotely executing the vs code JavaScript APIs outside of vscode which in turn will drive the GUI.

I am trying to figure out if I can do any of the following:

  • Execute the API JavaScript code when through the code cli
  • Run a web server/socket within an extension to listen to events from external systems and execute the JS API accordingly
  • If not socket/rest server, should I listen to filesystem changes and react accordingly?

Basically, what are the options for remote control driving of vs code?

like image 311
Owen Avatar asked Oct 20 '25 04:10

Owen


2 Answers

I've faced a similar problem with an editor extension I'm working on.

I would highly recommend listening to file changes outside of VSCode APIs. This way the majority of your code is not bound to a specific editor. I also had much more difficulty using the VSCode API called createFileSystemWatcher as it didn't capture all changes.

Upon activation, run a child process using a file watcher library. I'd recommend using chokidar - its the same library VSCode uses internally but with more extensive access to the API.

// get your workspace uri & root uri 
const workspaceRoots = vscode.workspace.workspaceFolders
if (!workspaceRoots || !workspaceRoots.length) {
  throw new Error('No workspace root path')
}
const workspaceRoot: vscode.WorkspaceFolder = workspaceRoots[0]
const rootUri = vscode.workspace.getWorkspaceFolder(workspaceUri);

// the action you want to trigger
const command = vscode.commands.registerCommand('YOUR_CUSTOM_COMMAND', yourCustomFunction)

// file watcher
const fsWatcher = chokidar.watch('watcher_name', {
  cwd: rootUri.uri.path,
  interval: 1000,
})

// listen to any add/update/delete fs events
fsWatcher.on('change', (path, event) => {
  vscode.commands.executeCommand('YOUR_CUSTOM_COMMAND')
})

On any file change, it can trigger the action of your choice.

like image 89
shmck Avatar answered Oct 23 '25 00:10

shmck


If a VS Code extension can achieve everything you are after, then the simplest approach would be to let the extension be driven externally. VS Code extensions run in a node.js environment, so they can start a http server, listen on a named pipe, or communicate with the outside world using pretty much any other mechanism.

You can also take a look at VS Code support for testing extensions, since tests also just use standard VS Code extension apis: https://code.visualstudio.com/api/working-with-extensions/testing-extension

For the development of VS Code itself, there are some automation scripts that simulate user actions. If you really need to, I believe you could adopt these script to your use case, however I can't say how much work it would be. These script also break if the UI changes. Better to go the extension route if possible

like image 43
Matt Bierner Avatar answered Oct 23 '25 01:10

Matt Bierner