Skip to content

Commit

Permalink
feat: allow to configure lsp features
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Dec 19, 2022
1 parent 3cdebc9 commit 340ba9b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/clarity-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "1.0.0"
lazy_static = "1.4.0"
lsp-types = "0.93.0"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0"
chainhook_types = { package = "chainhook-types", path = "../chainhook-types-rs" }
clarinet_files = { package = "clarinet-files", path = "../clarinet-files", default-features = false }
clarity_repl = { package = "clarity-repl", path = "../clarity-repl", default-features = false, optional = true }
Expand Down
74 changes: 51 additions & 23 deletions components/clarity-lsp/src/common/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,60 @@ pub enum LspRequestResponse {
Hover(Option<Hover>),
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializationOptions {
completion: bool,
hover: bool,
document_symbols: bool,
go_to_definition: bool,
}

pub fn process_request(command: LspRequest, editor_state: &EditorStateInput) -> LspRequestResponse {
match command {
LspRequest::Initialize(_params) => LspRequestResponse::Initialize(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
open_close: Some(true),
change: Some(TextDocumentSyncKind::FULL),
will_save: Some(false),
will_save_wait_until: Some(false),
save: Some(TextDocumentSyncSaveOptions::Supported(true)),
LspRequest::Initialize(params) => {
let initialization_options: InitializationOptions = params
.initialization_options
.and_then(|o| serde_json::from_str(o.as_str()?).ok())
.expect("failed to parse initialization options");

LspRequestResponse::Initialize(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
open_close: Some(true),
change: Some(TextDocumentSyncKind::FULL),
will_save: Some(false),
will_save_wait_until: Some(false),
save: Some(TextDocumentSyncSaveOptions::Supported(true)),
},
)),
completion_provider: match initialization_options.completion {
true => Some(CompletionOptions {
resolve_provider: Some(false),
trigger_characters: None,
all_commit_characters: None,
work_done_progress_options: Default::default(),
}),
false => None,
},
)),
completion_provider: Some(CompletionOptions {
resolve_provider: Some(false),
trigger_characters: None,
all_commit_characters: None,
work_done_progress_options: Default::default(),
}),
hover_provider: Some(HoverProviderCapability::Simple(true)),
document_symbol_provider: Some(lsp_types::OneOf::Left(true)),
definition_provider: Some(lsp_types::OneOf::Left(true)),
..ServerCapabilities::default()
},
}),
hover_provider: match initialization_options.hover {
true => Some(HoverProviderCapability::Simple(true)),
false => None,
},
document_symbol_provider: match initialization_options.document_symbols {
true => Some(lsp_types::OneOf::Left(true)),
false => None,
},
definition_provider: match initialization_options.go_to_definition {
true => Some(lsp_types::OneOf::Left(true)),
false => None,
},
..ServerCapabilities::default()
},
})
}

LspRequest::Completion(params) => {
let file_url = params.text_document_position.text_document.uri;
Expand Down
27 changes: 25 additions & 2 deletions components/clarity-vscode/client/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const clientOpts: LanguageClientOptions = {
traceOutputChannel: vscode.window.createOutputChannel(
"Clarity Language Server Trace",
),
initializationOptions: JSON.stringify(
workspace.getConfiguration("clarity-lsp"),
),
};

declare const __DEV_MODE__: boolean | undefined;
Expand Down Expand Up @@ -49,8 +52,28 @@ export async function initClient(
),
);

workspace.onDidChangeConfiguration((e) => {
config = workspace.getConfiguration("clarity-lsp");
workspace.onDidChangeConfiguration(async () => {
let requireReload = false;
let newConfig = workspace.getConfiguration("clarity-lsp");
["completion", "hover", "documentSymbols", "goToDefinition"].forEach(
(k) => {
if (newConfig[k] !== config[k]) requireReload = true;
},
);

config = newConfig;

if (requireReload) {
const userResponse = await vscode.window.showInformationMessage(
"Changing Clarity configuration requires to reload VSCode",
"Reload VSCode",
);

if (userResponse) {
const command = "workbench.action.reloadWindow";
await vscode.commands.executeCommand(command);
}
}
});

/* clariy lsp */
Expand Down
22 changes: 21 additions & 1 deletion components/clarity-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"homepage": "https://github.com/hirosystems/clarinet",
"bugs": "https://github.com/hirosystems/clarinet/issues",
"license": "GPL-3.0-only",
"version": "1.2.2",
"version": "1.3.0",
"workspaces": [
"client",
"server",
Expand Down Expand Up @@ -77,6 +77,26 @@
],
"default": "verbose",
"description": "Traces the communication between VS Code and the web-extension language server."
},
"clarity-lsp.completion": {
"type": "boolean",
"default": true,
"description": "Allow auto completion for native and user-defined functions"
},
"clarity-lsp.hover": {
"type": "boolean",
"default": true,
"description": "Show documentation for native function and keywords on hover"
},
"clarity-lsp.documentSymbols": {
"type": "boolean",
"default": false,
"description": "Show contract symbols in breadcrumb (beta)"
},
"clarity-lsp.goToDefinition": {
"type": "boolean",
"default": true,
"description": "Enable go to definition"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/clarity-vscode/server/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function initConnection(
connection: Connection,
bridge: LspVscodeBridge,
) {
connection.onInitialize(async (params) =>
connection.onInitialize((params) =>
bridge.onRequest(InitializeRequest.method, params),
);

Expand Down

0 comments on commit 340ba9b

Please sign in to comment.