From a30bdd9795770329e4562d8bfca60ebe2e52dea1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 25 May 2020 14:56:26 +0200 Subject: Cleanup lsp extensions on the client side --- crates/rust-analyzer/src/lsp_ext.rs | 2 +- crates/rust-analyzer/src/main_loop/handlers.rs | 11 +-- docs/dev/lsp-extensions.md | 2 +- editors/code/src/commands.ts | 2 +- editors/code/src/debug.ts | 2 +- editors/code/src/inlay_hints.ts | 2 +- editors/code/src/lsp_ext.ts | 84 +++++++++++++++++ editors/code/src/run.ts | 2 +- editors/code/src/rust-analyzer-api.ts | 123 ------------------------- 9 files changed, 93 insertions(+), 137 deletions(-) create mode 100644 editors/code/src/lsp_ext.ts delete mode 100644 editors/code/src/rust-analyzer-api.ts diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 1cce1baa4..c571c62ae 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -50,7 +50,7 @@ impl Request for ExpandMacro { #[serde(rename_all = "camelCase")] pub struct ExpandMacroParams { pub text_document: TextDocumentIdentifier, - pub position: Option, + pub position: Position, } #[derive(Deserialize, Serialize, Debug)] diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index a13a0e1f5..3ccc95c23 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -72,15 +72,10 @@ pub fn handle_expand_macro( let _p = profile("handle_expand_macro"); let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; let line_index = world.analysis().file_line_index(file_id)?; - let offset = params.position.map(|p| from_proto::offset(&line_index, p)); + let offset = from_proto::offset(&line_index, params.position); - match offset { - None => Ok(None), - Some(offset) => { - let res = world.analysis().expand_macro(FilePosition { file_id, offset })?; - Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion })) - } - } + let res = world.analysis().expand_macro(FilePosition { file_id, offset })?; + Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion })) } pub fn handle_selection_range( diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index e4b9fb2c2..48147b173 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -318,7 +318,7 @@ Primarily for debugging, but very useful for all people working on rust-analyzer ```typescript interface ExpandMacroParams { textDocument: TextDocumentIdentifier, - position?: Position, + position: Position, } ``` diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index e08030140..49e3845d5 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import * as ra from './rust-analyzer-api'; +import * as ra from './lsp_ext'; import { Ctx, Cmd } from './ctx'; import { applySnippetWorkspaceEdit, applySnippetTextEdits } from './snippets'; diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index d3fe588e8..027504ecd 100644 --- a/editors/code/src/debug.ts +++ b/editors/code/src/debug.ts @@ -1,7 +1,7 @@ import * as os from "os"; import * as vscode from 'vscode'; import * as path from 'path'; -import * as ra from './rust-analyzer-api'; +import * as ra from './lsp_ext'; import { Cargo } from './cargo'; import { Ctx } from "./ctx"; diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index a2b07d003..9e6d6045f 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -1,6 +1,6 @@ import * as lc from "vscode-languageclient"; import * as vscode from 'vscode'; -import * as ra from './rust-analyzer-api'; +import * as ra from './lsp_ext'; import { Ctx, Disposable } from './ctx'; import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util'; diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts new file mode 100644 index 000000000..2a0663261 --- /dev/null +++ b/editors/code/src/lsp_ext.ts @@ -0,0 +1,84 @@ +/** + * This file mirrors `crates/rust-analyzer/src/req.rs` declarations. + */ + +import * as lc from "vscode-languageclient"; + +export const analyzerStatus = new lc.RequestType("rust-analyzer/analyzerStatus"); + +export const collectGarbage = new lc.RequestType("rust-analyzer/collectGarbage"); + +export interface SyntaxTreeParams { + textDocument: lc.TextDocumentIdentifier; + range: lc.Range | null; +} +export const syntaxTree = new lc.RequestType("rust-analyzer/syntaxTree"); + + +export interface ExpandMacroParams { + textDocument: lc.TextDocumentIdentifier; + position: lc.Position; +} +export interface ExpandedMacro { + name: string; + expansion: string; +} +export const expandMacro = new lc.RequestType("rust-analyzer/expandMacro"); + +export interface MatchingBraceParams { + textDocument: lc.TextDocumentIdentifier; + positions: lc.Position[]; +} +export const matchingBrace = new lc.RequestType("experimental/matchingBrace"); + +export const parentModule = new lc.RequestType("rust-analyzer/parentModule"); + +export interface 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 { + textDocument: lc.TextDocumentIdentifier; + position: lc.Position | null; +} +export interface Runnable { + range: lc.Range; + label: string; + bin: string; + args: string[]; + extraArgs: string[]; + env: { [key: string]: string }; + cwd: string | null; +} +export const runnables = new lc.RequestType("rust-analyzer/runnables"); + +export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; + +export namespace InlayHint { + export const enum Kind { + TypeHint = "TypeHint", + ParamHint = "ParameterHint", + ChainingHint = "ChainingHint", + } + interface Common { + range: lc.Range; + label: string; + } + export type TypeHint = Common & { kind: Kind.TypeHint }; + export type ParamHint = Common & { kind: Kind.ParamHint }; + export type ChainingHint = Common & { kind: Kind.ChainingHint }; +} +export interface InlayHintsParams { + textDocument: lc.TextDocumentIdentifier; +} +export const inlayHints = new lc.RequestType("rust-analyzer/inlayHints"); + +export interface SsrParams { + query: string; + parseOnly: boolean; +} +export const ssr = new lc.RequestType('experimental/ssr'); diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts index 8f0487d74..2a7a429cf 100644 --- a/editors/code/src/run.ts +++ b/editors/code/src/run.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import * as ra from './rust-analyzer-api'; +import * as ra from './lsp_ext'; import { Ctx, Cmd } from './ctx'; import { startDebugSession, getDebugConfiguration } from './debug'; diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts deleted file mode 100644 index c10c0fa78..000000000 --- a/editors/code/src/rust-analyzer-api.ts +++ /dev/null @@ -1,123 +0,0 @@ -/** - * This file mirrors `crates/rust-analyzer/src/req.rs` declarations. - */ - -import * as lc from "vscode-languageclient"; - -type Option = null | T; -type Vec = T[]; -type FxHashMap = Record; - -function request(method: string) { - return new lc.RequestType(`rust-analyzer/${method}`); -} -function notification(method: string) { - return new lc.NotificationType(method); -} - - -export const analyzerStatus = request("analyzerStatus"); - - -export const collectGarbage = request("collectGarbage"); - - -export interface SyntaxTreeParams { - textDocument: lc.TextDocumentIdentifier; - range: Option; -} -export const syntaxTree = request("syntaxTree"); - - -export interface ExpandMacroParams { - textDocument: lc.TextDocumentIdentifier; - position: Option; -} -export interface ExpandedMacro { - name: string; - expansion: string; -} -export const expandMacro = request>("expandMacro"); - - -export interface MatchingBraceParams { - textDocument: lc.TextDocumentIdentifier; - positions: lc.Position[]; -} -export const matchingBrace = new lc.RequestType('experimental/matchingBrace'); - -export interface PublishDecorationsParams { - uri: string; - decorations: Vec; -} -export interface Decoration { - range: lc.Range; - tag: string; - bindingHash: Option; -} -export const decorationsRequest = request>("decorationsRequest"); - - -export const parentModule = request>("parentModule"); - - -export interface 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 { - textDocument: lc.TextDocumentIdentifier; - position: Option; -} -export interface Runnable { - range: lc.Range; - label: string; - bin: string; - args: Vec; - extraArgs: Vec; - env: FxHashMap; - cwd: Option; -} -export const runnables = request>("runnables"); - -export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; - -export namespace InlayHint { - export const enum Kind { - TypeHint = "TypeHint", - ParamHint = "ParameterHint", - ChainingHint = "ChainingHint", - } - interface Common { - range: lc.Range; - label: string; - } - export type TypeHint = Common & { kind: Kind.TypeHint }; - export type ParamHint = Common & { kind: Kind.ParamHint }; - export type ChainingHint = Common & { kind: Kind.ChainingHint }; -} -export interface InlayHintsParams { - textDocument: lc.TextDocumentIdentifier; -} -export const inlayHints = request>("inlayHints"); - - -export interface SsrParams { - query: string; - parseOnly: boolean; -} -export const ssr = new lc.RequestType('experimental/ssr'); - - -export const publishDecorations = notification("publishDecorations"); - - -export interface SourceChange { - label: string; - workspaceEdit: lc.WorkspaceEdit; - cursorPosition: Option; -} -- cgit v1.2.3