diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 1cce1baa455..c571c62aed3 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 a13a0e1f523..3ccc95c23e0 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 e4b9fb2c25e..48147b17393 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 e080301405d..49e3845d5b1 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 d3fe588e831..027504ecd2a 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 a2b07d00378..9e6d6045f34 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 00000000000..2a06632619b --- /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 8f0487d74a1..2a7a429cfa6 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 c10c0fa7894..00000000000 --- 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; -}