0

I created an Xtext grammar and Language server in eclipse, this was built and added to a vscode extension. I have implemented the below code to start up the language server.

import * as path from 'path';
import { workspace, ExtensionContext } from 'vscode';

import {
    LanguageClient,
    LanguageClientOptions,
    ServerOptions,
    Trace,
    TransportKind
} from 'vscode-languageclient/node'; // Use this dependency for version >= 7.0.0

export function activate(context: ExtensionContext) {
    
    const executable = 'prototype.dsl.ide.bat';
    const languageServerPath =  path.join('server', 'bin', executable);

    const serverLauncher = context.asAbsolutePath(languageServerPath);
    const serverOptions: ServerOptions = {
        run: {
            command: serverLauncher,
            args: ['-trace', '-log', 'debug'],
            transport: TransportKind.ipc
        },
        debug: {
            command: serverLauncher,
            args: ['-trace', '-log', 'debug'],
            transport: TransportKind.ipc
        }
    };

    const config = workspace.getConfiguration();

    const clientOptions: LanguageClientOptions = {
        documentSelector: [{ scheme: 'file', language: 'tst' }],
        synchronize: {
            fileEvents: workspace.createFileSystemWatcher('**/*.*,**/*.tst')
        },
        initializationOptions: {
            config
        }
    };

    const languageClient = new LanguageClient('lspLanguageClient', 'Language Server', serverOptions, clientOptions);
    languageClient.start();

    languageClient.trace = Trace.Verbose;
    let disposable = languageClient.start();
    
    context.subscriptions.push(disposable);
}

Testing the implementation in vscode works and the grammar rules are detected.

The issue I am having is that in vscode on save the changes to the document only reflect in the language server the first time, all subsequent modifications and saves are not reflected in the language server.

Reece
  • 1
  • 1

1 Answers1

0

I managed to solve my issue, the server was accessing the document resource using the below method.

@Override
public void didSave(DidSaveTextDocumentParams params) {

    URI uri = URI.createURI(params.getTextDocument().getUri());

    XtextResource resource = (XtextResource) getWorkspaceManager().getProjectManager(uri).getResource(uri);

    EList<EObject> modelObjects = resource.getContents();

    System.out.println("SAVING MODEL " + " (" + resource.getContents().size() + ") -> "
            + ((Greetings) modelObjects.get(0)).getGreetings().get(1).getName())


}

The above code works when testing in eclipse, but not when used in a a vscode extension.

I changed the above code to get the resource using the following and it worked in both eclipse and vscode extension.

...
getLanguageServerAccess().doRead(params.getTextDocument().getUri(),
    (ILanguageServerAccess.Context context) -> {
        Resource resource = context.getResource();
        System.out.println("SAVING MODEL " + " (" + resource.getContents().size() + ") -> "
            + ((Greetings) modelObjects.get(0)).getGreetings().get(1).getName())
        return null;
    });
...
Reece
  • 1
  • 1