From 64110714f0b51c31c0c1a48717b725328f6730ba Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 23 Jan 2023 15:26:28 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20lsp=5Fext.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- editors/code/src/client.ts | 43 +++---- editors/code/src/commands.ts | 4 +- editors/code/src/lsp_ext.ts | 241 ++++++++++++++++------------------- 3 files changed, 130 insertions(+), 158 deletions(-) diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 9335e9abb30..62980ca0464 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -172,12 +172,10 @@ export async function createClient( ) .then( (result) => { + if (!result) return null; const hover = client.protocol2CodeConverter.asHover(result); - if (hover) { - const actions = (result).actions; - if (actions) { - hover.contents.push(renderHoverActions(actions)); - } + if (!!result.actions) { + hover.contents.push(renderHoverActions(result.actions)); } return hover; }, @@ -310,26 +308,27 @@ class ExperimentalFeatures implements lc.StaticFeature { return { kind: "static" }; } fillClientCapabilities(capabilities: lc.ClientCapabilities): void { - const caps: any = capabilities.experimental ?? {}; - caps.snippetTextEdit = true; - caps.codeActionGroup = true; - caps.hoverActions = true; - caps.serverStatusNotification = true; - caps.colorDiagnosticOutput = true; - caps.openServerLogs = true; - caps.commands = { - commands: [ - "rust-analyzer.runSingle", - "rust-analyzer.debugSingle", - "rust-analyzer.showReferences", - "rust-analyzer.gotoLocation", - "editor.action.triggerParameterHints", - ], + capabilities.experimental = { + snippetTextEdit: true, + codeActionGroup: true, + hoverActions: true, + serverStatusNotification: true, + colorDiagnosticOutput: true, + openServerLogs: true, + commands: { + commands: [ + "rust-analyzer.runSingle", + "rust-analyzer.debugSingle", + "rust-analyzer.showReferences", + "rust-analyzer.gotoLocation", + "editor.action.triggerParameterHints", + ], + }, + ...capabilities.experimental, }; - capabilities.experimental = caps; } initialize( - _capabilities: lc.ServerCapabilities, + _capabilities: lc.ServerCapabilities, _documentSelector: lc.DocumentSelector | undefined ): void {} dispose(): void {} diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index cb4e13e2c60..b5b64e33e07 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -130,11 +130,11 @@ export function joinLines(ctx: CtxInit): Cmd { } export function moveItemUp(ctx: CtxInit): Cmd { - return moveItem(ctx, ra.Direction.Up); + return moveItem(ctx, "Up"); } export function moveItemDown(ctx: CtxInit): Cmd { - return moveItem(ctx, ra.Direction.Down); + return moveItem(ctx, "Down"); } export function moveItem(ctx: CtxInit, direction: ra.Direction): Cmd { diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 6ddf83799c0..f6f5124dc41 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -4,131 +4,134 @@ import * as lc from "vscode-languageclient"; -export interface AnalyzerStatusParams { - textDocument?: lc.TextDocumentIdentifier; -} +// rust-analyzer overrides + +export const hover = new lc.RequestType< + HoverParams, + (lc.Hover & { actions: CommandLinkGroup[] }) | null, + void +>("textDocument/hover"); +export type HoverParams = { position: lc.Position | lc.Range } & Omit< + lc.TextDocumentPositionParams, + "position" +> & + lc.WorkDoneProgressParams; +export type CommandLink = { + /** + * A tooltip for the command, when represented in the UI. + */ + tooltip?: string; +} & lc.Command; +export type CommandLinkGroup = { + title?: string; + commands: CommandLink[]; +}; + +// rust-analyzer extensions + export const analyzerStatus = new lc.RequestType( "rust-analyzer/analyzerStatus" ); -export const memoryUsage = new lc.RequestType0("rust-analyzer/memoryUsage"); -export const shuffleCrateGraph = new lc.RequestType0("rust-analyzer/shuffleCrateGraph"); - -export interface ServerStatusParams { - health: "ok" | "warning" | "error"; - quiescent: boolean; - message?: string; -} -export const serverStatus = new lc.NotificationType( - "experimental/serverStatus" +export const cancelFlycheck = new lc.NotificationType0("rust-analyzer/cancelFlycheck"); +export const clearFlycheck = new lc.NotificationType0("rust-analyzer/clearFlycheck"); +export const expandMacro = new lc.RequestType( + "rust-analyzer/expandMacro" ); +export const memoryUsage = new lc.RequestType0("rust-analyzer/memoryUsage"); export const openServerLogs = new lc.NotificationType0("rust-analyzer/openServerLogs"); - +export const relatedTests = new lc.RequestType( + "rust-analyzer/relatedTests" +); export const reloadWorkspace = new lc.RequestType0("rust-analyzer/reloadWorkspace"); - -export const hover = new lc.RequestType("textDocument/hover"); - -export interface HoverParams extends lc.WorkDoneProgressParams { - textDocument: lc.TextDocumentIdentifier; - position: lc.Range | lc.Position; -} - -export interface SyntaxTreeParams { - textDocument: lc.TextDocumentIdentifier; - range: lc.Range | null; -} +export const runFlycheck = new lc.NotificationType<{ + textDocument: lc.TextDocumentIdentifier | null; +}>("rust-analyzer/runFlycheck"); +export const shuffleCrateGraph = new lc.RequestType0("rust-analyzer/shuffleCrateGraph"); export const syntaxTree = new lc.RequestType( "rust-analyzer/syntaxTree" ); - -export const viewHir = new lc.RequestType( - "rust-analyzer/viewHir" +export const viewCrateGraph = new lc.RequestType( + "rust-analyzer/viewCrateGraph" ); - export const viewFileText = new lc.RequestType( "rust-analyzer/viewFileText" ); - -export interface ViewItemTreeParams { - textDocument: lc.TextDocumentIdentifier; -} - +export const viewHir = new lc.RequestType( + "rust-analyzer/viewHir" +); export const viewItemTree = new lc.RequestType( "rust-analyzer/viewItemTree" ); -export interface ViewCrateGraphParams { - full: boolean; -} +export type AnalyzerStatusParams = { textDocument?: lc.TextDocumentIdentifier }; -export const viewCrateGraph = new lc.RequestType( - "rust-analyzer/viewCrateGraph" -); - -export interface ExpandMacroParams { +export type ExpandMacroParams = { textDocument: lc.TextDocumentIdentifier; position: lc.Position; -} -export interface ExpandedMacro { +}; +export type ExpandedMacro = { name: string; expansion: string; -} -export const expandMacro = new lc.RequestType( - "rust-analyzer/expandMacro" -); - -export const relatedTests = new lc.RequestType( - "rust-analyzer/relatedTests" -); - -export const cancelFlycheck = new lc.NotificationType0("rust-analyzer/cancelFlycheck"); -export const clearFlycheck = new lc.NotificationType0("rust-analyzer/clearFlycheck"); -export const runFlycheck = new lc.NotificationType<{ - textDocument: lc.TextDocumentIdentifier | null; -}>("rust-analyzer/runFlycheck"); - -// Experimental extensions - -export interface SsrParams { - query: string; - parseOnly: boolean; +}; +export type TestInfo = { runnable: Runnable }; +export type SyntaxTreeParams = { textDocument: lc.TextDocumentIdentifier; - position: lc.Position; - selections: readonly lc.Range[]; -} -export const ssr = new lc.RequestType("experimental/ssr"); + range: lc.Range | null; +}; +export type ViewCrateGraphParams = { full: boolean }; +export type ViewItemTreeParams = { textDocument: lc.TextDocumentIdentifier }; -export interface MatchingBraceParams { - textDocument: lc.TextDocumentIdentifier; - positions: lc.Position[]; -} +// experimental extensions + +export const joinLines = new lc.RequestType( + "experimental/joinLines" +); export const matchingBrace = new lc.RequestType( "experimental/matchingBrace" ); - +export const moveItem = new lc.RequestType( + "experimental/moveItem" +); +export const onEnter = new lc.RequestType( + "experimental/onEnter" +); +export const openCargoToml = new lc.RequestType( + "experimental/openCargoToml" +); +export const openDocs = new lc.RequestType( + "experimental/externalDocs" +); export const parentModule = new lc.RequestType< lc.TextDocumentPositionParams, lc.LocationLink[] | null, void >("experimental/parentModule"); +export const runnables = new lc.RequestType( + "experimental/runnables" +); +export const serverStatus = new lc.NotificationType( + "experimental/serverStatus" +); +export const ssr = new lc.RequestType("experimental/ssr"); -export interface JoinLinesParams { +export type JoinLinesParams = { textDocument: lc.TextDocumentIdentifier; ranges: lc.Range[]; -} -export const joinLines = new lc.RequestType( - "experimental/joinLines" -); - -export const onEnter = new lc.RequestType( - "experimental/onEnter" -); - -export interface RunnablesParams { +}; +export type MatchingBraceParams = { textDocument: lc.TextDocumentIdentifier; - position: lc.Position | null; -} - -export interface Runnable { + positions: lc.Position[]; +}; +export type MoveItemParams = { + textDocument: lc.TextDocumentIdentifier; + range: lc.Range; + direction: Direction; +}; +export type Direction = "Up" | "Down"; +export type OpenCargoTomlParams = { + textDocument: lc.TextDocumentIdentifier; +}; +export type Runnable = { label: string; location?: lc.LocationLink; kind: "cargo"; @@ -140,50 +143,20 @@ export interface Runnable { expectTest?: boolean; overrideCargo?: string; }; -} -export const runnables = new lc.RequestType( - "experimental/runnables" -); - -export interface TestInfo { - runnable: Runnable; -} - -export interface CommandLink extends lc.Command { - /** - * A tooltip for the command, when represented in the UI. - */ - tooltip?: string; -} - -export interface CommandLinkGroup { - title?: string; - commands: CommandLink[]; -} - -export const openDocs = new lc.RequestType( - "experimental/externalDocs" -); - -export const openCargoToml = new lc.RequestType( - "experimental/openCargoToml" -); - -export interface OpenCargoTomlParams { +}; +export type RunnablesParams = { textDocument: lc.TextDocumentIdentifier; -} - -export const moveItem = new lc.RequestType( - "experimental/moveItem" -); - -export interface MoveItemParams { + position: lc.Position | null; +}; +export type ServerStatusParams = { + health: "ok" | "warning" | "error"; + quiescent: boolean; + message?: string; +}; +export type SsrParams = { + query: string; + parseOnly: boolean; textDocument: lc.TextDocumentIdentifier; - range: lc.Range; - direction: Direction; -} - -export const enum Direction { - Up = "Up", - Down = "Down", -} + position: lc.Position; + selections: readonly lc.Range[]; +};