mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
Fix linter issues
This commit is contained in:
parent
f7b8ae1ee7
commit
583f5c9612
3 changed files with 72 additions and 32 deletions
|
@ -1,12 +1,12 @@
|
|||
import * as analyzerStatus from './analyzer_status';
|
||||
import * as applySourceChange from './apply_source_change';
|
||||
import * as inlayHints from './inlay_hints';
|
||||
import * as joinLines from './join_lines';
|
||||
import * as matchingBrace from './matching_brace';
|
||||
import * as onEnter from './on_enter';
|
||||
import * as parentModule from './parent_module';
|
||||
import * as runnables from './runnables';
|
||||
import * as syntaxTree from './syntaxTree';
|
||||
import * as inlayHints from './inlay_hints';
|
||||
|
||||
export {
|
||||
analyzerStatus,
|
||||
|
@ -17,5 +17,5 @@ export {
|
|||
runnables,
|
||||
syntaxTree,
|
||||
onEnter,
|
||||
inlayHints,
|
||||
inlayHints
|
||||
};
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
import * as vscode from 'vscode';
|
||||
import { Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode';
|
||||
import {
|
||||
Range,
|
||||
TextDocumentChangeEvent,
|
||||
TextDocumentContentChangeEvent,
|
||||
TextEditor
|
||||
} from 'vscode';
|
||||
import { TextDocumentIdentifier } from 'vscode-languageclient';
|
||||
import { Server } from '../server';
|
||||
|
||||
|
@ -8,23 +13,28 @@ interface InlayHintsParams {
|
|||
}
|
||||
|
||||
interface InlayHint {
|
||||
range: Range,
|
||||
kind: string,
|
||||
label: string,
|
||||
range: Range;
|
||||
kind: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
after: {
|
||||
color: new vscode.ThemeColor('ralsp.inlayHint'),
|
||||
},
|
||||
color: new vscode.ThemeColor('ralsp.inlayHint')
|
||||
}
|
||||
});
|
||||
|
||||
export class HintsUpdater {
|
||||
private displayHints = true;
|
||||
|
||||
public async loadHints(editor: vscode.TextEditor | undefined): Promise<void> {
|
||||
public async loadHints(
|
||||
editor: vscode.TextEditor | undefined
|
||||
): Promise<void> {
|
||||
if (this.displayHints && editor !== undefined) {
|
||||
await this.updateDecorationsFromServer(editor.document.uri.toString(), editor);
|
||||
await this.updateDecorationsFromServer(
|
||||
editor.document.uri.toString(),
|
||||
editor
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +47,7 @@ export class HintsUpdater {
|
|||
} else {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor != null) {
|
||||
return editor.setDecorations(typeHintDecorationType, [])
|
||||
return editor.setDecorations(typeHintDecorationType, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -58,38 +68,62 @@ export class HintsUpdater {
|
|||
|
||||
// If the dbg! macro is used in the lsp-server, an endless stream of events with `cause.contentChanges` with the dbg messages.
|
||||
// Should not be a real situation, but better to filter such things out.
|
||||
if (cause !== undefined && cause.contentChanges.filter(changeEvent => this.isEventInFile(document.lineCount, changeEvent)).length === 0) {
|
||||
if (
|
||||
cause !== undefined &&
|
||||
cause.contentChanges.filter(changeEvent =>
|
||||
this.isEventInFile(document.lineCount, changeEvent)
|
||||
).length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
return await this.updateDecorationsFromServer(document.uri.toString(), editor);
|
||||
return await this.updateDecorationsFromServer(
|
||||
document.uri.toString(),
|
||||
editor
|
||||
);
|
||||
}
|
||||
|
||||
private isEventInFile(documentLineCount: number, event: TextDocumentContentChangeEvent): boolean {
|
||||
private isEventInFile(
|
||||
documentLineCount: number,
|
||||
event: TextDocumentContentChangeEvent
|
||||
): boolean {
|
||||
const eventText = event.text;
|
||||
if (eventText.length === 0) {
|
||||
return event.range.start.line <= documentLineCount || event.range.end.line <= documentLineCount;
|
||||
return (
|
||||
event.range.start.line <= documentLineCount ||
|
||||
event.range.end.line <= documentLineCount
|
||||
);
|
||||
} else {
|
||||
return event.range.start.line <= documentLineCount && event.range.end.line <= documentLineCount;
|
||||
return (
|
||||
event.range.start.line <= documentLineCount &&
|
||||
event.range.end.line <= documentLineCount
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async updateDecorationsFromServer(documentUri: string, editor: TextEditor): Promise<void> {
|
||||
const newHints = await this.queryHints(documentUri) || [];
|
||||
const newDecorations = newHints.map(hint => (
|
||||
{
|
||||
range: hint.range,
|
||||
renderOptions: { after: { contentText: `: ${hint.label}` } },
|
||||
}
|
||||
));
|
||||
private async updateDecorationsFromServer(
|
||||
documentUri: string,
|
||||
editor: TextEditor
|
||||
): Promise<void> {
|
||||
const newHints = (await this.queryHints(documentUri)) || [];
|
||||
const newDecorations = newHints.map(hint => ({
|
||||
range: hint.range,
|
||||
renderOptions: { after: { contentText: `: ${hint.label}` } }
|
||||
}));
|
||||
return editor.setDecorations(typeHintDecorationType, newDecorations);
|
||||
}
|
||||
|
||||
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
||||
const request: InlayHintsParams = { textDocument: { uri: documentUri } };
|
||||
const request: InlayHintsParams = {
|
||||
textDocument: { uri: documentUri }
|
||||
};
|
||||
const client = Server.client;
|
||||
return client.onReady().then(() => client.sendRequest<InlayHint[] | null>(
|
||||
'rust-analyzer/inlayHints',
|
||||
request
|
||||
));
|
||||
return client
|
||||
.onReady()
|
||||
.then(() =>
|
||||
client.sendRequest<InlayHint[] | null>(
|
||||
'rust-analyzer/inlayHints',
|
||||
request
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -152,9 +152,15 @@ export function activate(context: vscode.ExtensionContext) {
|
|||
if (Server.config.displayInlayHints) {
|
||||
const hintsUpdater = new HintsUpdater();
|
||||
hintsUpdater.loadHints(vscode.window.activeTextEditor).then(() => {
|
||||
vscode.window.onDidChangeActiveTextEditor(editor => hintsUpdater.loadHints(editor));
|
||||
vscode.workspace.onDidChangeTextDocument(e => hintsUpdater.updateHints(e));
|
||||
vscode.workspace.onDidChangeConfiguration(_ => hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints));
|
||||
vscode.window.onDidChangeActiveTextEditor(editor =>
|
||||
hintsUpdater.loadHints(editor)
|
||||
);
|
||||
vscode.workspace.onDidChangeTextDocument(e =>
|
||||
hintsUpdater.updateHints(e)
|
||||
);
|
||||
vscode.workspace.onDidChangeConfiguration(_ =>
|
||||
hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints)
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue