Skip to content

Commit 340ba9b

Browse files
committed
feat: allow to configure lsp features
1 parent 3cdebc9 commit 340ba9b

File tree

6 files changed

+100
-27
lines changed

6 files changed

+100
-27
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/clarity-lsp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "1.0.0"
77
lazy_static = "1.4.0"
88
lsp-types = "0.93.0"
99
serde = { version = "1.0.136", features = ["derive"] }
10+
serde_json = "1.0"
1011
chainhook_types = { package = "chainhook-types", path = "../chainhook-types-rs" }
1112
clarinet_files = { package = "clarinet-files", path = "../clarinet-files", default-features = false }
1213
clarity_repl = { package = "clarity-repl", path = "../clarity-repl", default-features = false, optional = true }

components/clarity-lsp/src/common/backend.rs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -277,32 +277,60 @@ pub enum LspRequestResponse {
277277
Hover(Option<Hover>),
278278
}
279279

280+
#[derive(Debug, Clone, Deserialize, Serialize)]
281+
#[serde(rename_all = "camelCase")]
282+
pub struct InitializationOptions {
283+
completion: bool,
284+
hover: bool,
285+
document_symbols: bool,
286+
go_to_definition: bool,
287+
}
288+
280289
pub fn process_request(command: LspRequest, editor_state: &EditorStateInput) -> LspRequestResponse {
281290
match command {
282-
LspRequest::Initialize(_params) => LspRequestResponse::Initialize(InitializeResult {
283-
server_info: None,
284-
capabilities: ServerCapabilities {
285-
text_document_sync: Some(TextDocumentSyncCapability::Options(
286-
TextDocumentSyncOptions {
287-
open_close: Some(true),
288-
change: Some(TextDocumentSyncKind::FULL),
289-
will_save: Some(false),
290-
will_save_wait_until: Some(false),
291-
save: Some(TextDocumentSyncSaveOptions::Supported(true)),
291+
LspRequest::Initialize(params) => {
292+
let initialization_options: InitializationOptions = params
293+
.initialization_options
294+
.and_then(|o| serde_json::from_str(o.as_str()?).ok())
295+
.expect("failed to parse initialization options");
296+
297+
LspRequestResponse::Initialize(InitializeResult {
298+
server_info: None,
299+
capabilities: ServerCapabilities {
300+
text_document_sync: Some(TextDocumentSyncCapability::Options(
301+
TextDocumentSyncOptions {
302+
open_close: Some(true),
303+
change: Some(TextDocumentSyncKind::FULL),
304+
will_save: Some(false),
305+
will_save_wait_until: Some(false),
306+
save: Some(TextDocumentSyncSaveOptions::Supported(true)),
307+
},
308+
)),
309+
completion_provider: match initialization_options.completion {
310+
true => Some(CompletionOptions {
311+
resolve_provider: Some(false),
312+
trigger_characters: None,
313+
all_commit_characters: None,
314+
work_done_progress_options: Default::default(),
315+
}),
316+
false => None,
292317
},
293-
)),
294-
completion_provider: Some(CompletionOptions {
295-
resolve_provider: Some(false),
296-
trigger_characters: None,
297-
all_commit_characters: None,
298-
work_done_progress_options: Default::default(),
299-
}),
300-
hover_provider: Some(HoverProviderCapability::Simple(true)),
301-
document_symbol_provider: Some(lsp_types::OneOf::Left(true)),
302-
definition_provider: Some(lsp_types::OneOf::Left(true)),
303-
..ServerCapabilities::default()
304-
},
305-
}),
318+
hover_provider: match initialization_options.hover {
319+
true => Some(HoverProviderCapability::Simple(true)),
320+
false => None,
321+
},
322+
document_symbol_provider: match initialization_options.document_symbols {
323+
true => Some(lsp_types::OneOf::Left(true)),
324+
false => None,
325+
},
326+
definition_provider: match initialization_options.go_to_definition {
327+
true => Some(lsp_types::OneOf::Left(true)),
328+
false => None,
329+
},
330+
..ServerCapabilities::default()
331+
},
332+
})
333+
}
306334

307335
LspRequest::Completion(params) => {
308336
let file_url = params.text_document_position.text_document.uri;

components/clarity-vscode/client/src/common.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export const clientOpts: LanguageClientOptions = {
1919
traceOutputChannel: vscode.window.createOutputChannel(
2020
"Clarity Language Server Trace",
2121
),
22+
initializationOptions: JSON.stringify(
23+
workspace.getConfiguration("clarity-lsp"),
24+
),
2225
};
2326

2427
declare const __DEV_MODE__: boolean | undefined;
@@ -49,8 +52,28 @@ export async function initClient(
4952
),
5053
);
5154

52-
workspace.onDidChangeConfiguration((e) => {
53-
config = workspace.getConfiguration("clarity-lsp");
55+
workspace.onDidChangeConfiguration(async () => {
56+
let requireReload = false;
57+
let newConfig = workspace.getConfiguration("clarity-lsp");
58+
["completion", "hover", "documentSymbols", "goToDefinition"].forEach(
59+
(k) => {
60+
if (newConfig[k] !== config[k]) requireReload = true;
61+
},
62+
);
63+
64+
config = newConfig;
65+
66+
if (requireReload) {
67+
const userResponse = await vscode.window.showInformationMessage(
68+
"Changing Clarity configuration requires to reload VSCode",
69+
"Reload VSCode",
70+
);
71+
72+
if (userResponse) {
73+
const command = "workbench.action.reloadWindow";
74+
await vscode.commands.executeCommand(command);
75+
}
76+
}
5477
});
5578

5679
/* clariy lsp */

components/clarity-vscode/package.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"homepage": "https://github.com/hirosystems/clarinet",
99
"bugs": "https://github.com/hirosystems/clarinet/issues",
1010
"license": "GPL-3.0-only",
11-
"version": "1.2.2",
11+
"version": "1.3.0",
1212
"workspaces": [
1313
"client",
1414
"server",
@@ -77,6 +77,26 @@
7777
],
7878
"default": "verbose",
7979
"description": "Traces the communication between VS Code and the web-extension language server."
80+
},
81+
"clarity-lsp.completion": {
82+
"type": "boolean",
83+
"default": true,
84+
"description": "Allow auto completion for native and user-defined functions"
85+
},
86+
"clarity-lsp.hover": {
87+
"type": "boolean",
88+
"default": true,
89+
"description": "Show documentation for native function and keywords on hover"
90+
},
91+
"clarity-lsp.documentSymbols": {
92+
"type": "boolean",
93+
"default": false,
94+
"description": "Show contract symbols in breadcrumb (beta)"
95+
},
96+
"clarity-lsp.goToDefinition": {
97+
"type": "boolean",
98+
"default": true,
99+
"description": "Enable go to definition"
80100
}
81101
}
82102
}

components/clarity-vscode/server/src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function initConnection(
1616
connection: Connection,
1717
bridge: LspVscodeBridge,
1818
) {
19-
connection.onInitialize(async (params) =>
19+
connection.onInitialize((params) =>
2020
bridge.onRequest(InitializeRequest.method, params),
2121
);
2222

0 commit comments

Comments
 (0)