monaco-languageclient icon indicating copy to clipboard operation
monaco-languageclient copied to clipboard

Error: Unable to read file

Open buglessbuild opened this issue 1 year ago • 6 comments

Hello team,

When I try to use Peek References feature to see all the references of the function declared in the code but it throws the below error message:

Unable to read file '/workspace/file.java' (Error: Unable to resolve nonexistent file '/workspace/file.java')

I’m using [email protected] and [email protected].

Code:

import * as monaco from 'monaco-editor';
import { initServices } from 'monaco-languageclient/vscode/services';
import { MonacoLanguageClient } from 'monaco-languageclient';
import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc';
import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient';

let editor = null;
let languageClient = null;
let webSocket = null;

const startClient = async (value, language, fileUri) => {
  await initServices({});

  editor = monaco.editor.create(editorNode, {
    minimap: {
      enabled: false,
    },
    theme: "vs-dark",
  });

  let editorModel = monaco.editor.createModel(
    value,
    language,
    monaco.Uri.parse(fileUri)
  );

  editor.setModel(editorModel);

  webSocket = initLspConnection(`ws://localhost:8080/${language}`);
};

const initLspConnection = (url, language): WebSocket => {
  webSocket = new WebSocket(url);

  webSocket.onopen = async () => {
    const socket = toSocket(webSocket);
    const reader = new WebSocketMessageReader(socket);
    const writer = new WebSocketMessageWriter(socket);
    languageClient = createLanguageClient({
      reader,
      writer,
    }, language);

    await languageClient.start();

    reader.onClose(async () => await languageClient.stop());
  };
  return webSocket;
};

const createLanguageClient = (transports, language) => {
  return new MonacoLanguageClient({
    name: "Sample Language Client",
    clientOptions: {
      workspaceFolder,
      documentSelector: [language],
      errorHandler: {
        error: () => ({ action: ErrorAction.Continue }),
        closed: () => ({ action: CloseAction.DoNotRestart }),
      },
    },
    connectionProvider: {
      get: () => {
        return Promise.resolve(transports);
      },
    },
  });
};

// calling this on language change
const onChange = async (language, fileUri, value) => {
  await dispose();

  let editorModel = monaco.editor.createModel(
    value,
    language,
    monaco.Uri.file(fileUri)
  );

  editor.setModel(editorModel);

  webSocket = initLspConnection(`ws://localhost:8080/${language}`);
};

const dispose = async () => {
  if (languageClient) {
    await languageClient.dispose();
    languageClient = null;
  }

  if (webSocket) {
    webSocket.close();
    webSocket = null;
  }
};

startClient("", 'java', myFilePath)

can you please tell me how can I enable the peek references feature? It works in official monaco editor by default but not with the monaco-vscode-api.

buglessbuild avatar Oct 14 '24 07:10 buglessbuild