Skip to content

Commit

Permalink
fix lsp crash when hovering and remove some unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Aug 5, 2024
1 parent 327853d commit 997f404
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 72 deletions.
4 changes: 2 additions & 2 deletions client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default function App() {
}, [appState.code, appState.settings]);

useEffect(() => {
lspClient.initialize(appState.settings);
lspClient.updateSettings(appState.settings);
}, [appState.settings]);

function onShowRightPanel(rightPanelType?: RightPanelType) {
Expand Down Expand Up @@ -160,7 +160,7 @@ export default function App() {
diagnostics={appState.diagnostics}
onUpdateCode={(code: string) => {
// Tell the LSP client about the code change.
lspClient.getDiagnostics(code)
lspClient.updateTextDocument(code)
lspClient.updateCode(code)
setAppState((prevState) => {
return { ...prevState, code, isProblemsPanelDisplayed: true };
Expand Down
93 changes: 30 additions & 63 deletions client/LspClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ const documentUri = rootUri + fileName
const workerScriptName = "worker.js";

export class LspClient {
public connection: MessageConnection;
public connection: MessageConnection | undefined;
public onNotification: (diagnostics: Diagnostic[]) => void
private _documentVersion = 1;
private _documentText = '';
private _documentDiags: PublishDiagnosticsParams | undefined;
private _pendingDiagRequests = new Map<number, DiagnosticRequest[]>();

constructor() {
}

public updateCode = (code: string) => [
this._documentText = code
]

public async initialize(sessionOptions?: SessionOptions) {
const workerScript = `./${workerScriptName}`;
const foreground = new Worker(workerScript, {
name: 'Pyright-foreground',
Expand All @@ -85,38 +92,32 @@ export class LspClient {
let backgroundWorkerCount = 0;
foreground.addEventListener("message", (e: MessageEvent) => {
if (e.data && e.data.type === "browser/newWorker") {
// Create a new background worker.
// The foreground worker has created a message channel and passed us
// a port. We create the background worker and pass transfer the port
// onward.
const { initialData, port } = e.data;
const background = new Worker(workerScript, {
name: `Pyright-background-${++backgroundWorkerCount}`,
});
workers.push(background);
background.postMessage(
{
type: "browser/boot",
mode: "background",
initialData,
port,
},
[port]
);
// Create a new background worker.
// The foreground worker has created a message channel and passed us
// a port. We create the background worker and pass transfer the port
// onward.
const { initialData, port } = e.data;
const background = new Worker(workerScript, {
name: `Pyright-background-${++backgroundWorkerCount}`,
});
workers.push(background);
background.postMessage(
{
type: "browser/boot",
mode: "background",
initialData,
port,
},
[port]
);
}
});


this.connection = connection

this.connection.listen();
}

public updateCode = (code: string) => [
this._documentText = code
]

public async initialize(sessionOptions?: SessionOptions) {
// Initialize the server.
const init: InitializeParams = {
rootUri,
Expand Down Expand Up @@ -209,43 +210,9 @@ export class LspClient {
);
}

async getDiagnostics(code: string): Promise<Diagnostic[]> {
const codeChanged = this._documentText !== code;

// If the code hasn't changed since the last time we received
// a code update, return the cached diagnostics.
if (!codeChanged && this._documentDiags) {
return this._documentDiags.diagnostics;
}

// The diagnostics will come back asynchronously, so
// return a promise.
return new Promise<Diagnostic[]>(async (resolve, reject) => {
let documentVersion = this._documentVersion;

if (codeChanged) {
documentVersion = await this.updateTextDocument(code);
}

// Queue a request for diagnostics.
let requestList = this._pendingDiagRequests.get(documentVersion);
if (!requestList) {
requestList = [];
this._pendingDiagRequests.set(documentVersion, requestList);
}

requestList.push({
callback: (diagnostics, err) => {
if (err) {
reject(err);
return;
}

console.info(`Diagnostic callback ${JSON.stringify(diagnostics)}}`);
resolve(diagnostics);
},
});
});
updateSettings = async (sessionOptions: SessionOptions) => {
this.connection?.dispose()
await this.initialize(sessionOptions)
}

async getHoverInfo(code: string, position: Position): Promise<Hover | null> {
Expand Down Expand Up @@ -361,7 +328,7 @@ export class LspClient {

// Sends a new version of the text document to the language server.
// It bumps the document version and returns the new version number.
private async updateTextDocument(code: string): Promise<number> {
async updateTextDocument(code: string): Promise<number> {
let documentVersion = ++this._documentVersion;
this._documentText = code;

Expand Down
10 changes: 3 additions & 7 deletions client/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,10 @@ async function handleHoverRequest(
if (!lspClient) {
return null;
}

try {
const hoverInfo = await lspClient.connection.sendRequest(HoverRequest.type, {
textDocument: {uri: model.uri.toString()},
position: {
line: position.lineNumber - 1,
character: position.column - 1
}
const hoverInfo = await lspClient.getHoverInfo(model.getValue(), {
line: position.lineNumber - 1,
character: position.column - 1,
});

return {
Expand Down

0 comments on commit 997f404

Please sign in to comment.