Skip to content

Commit

Permalink
feat: watching configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu authored and benfdking committed Jun 20, 2024
1 parent 55652fa commit 6d0c97b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
80 changes: 45 additions & 35 deletions crates/lsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use ahash::AHashMap;
use lsp_server::{Connection, Message, Request, RequestId, Response};
use lsp_types::notification::{
Expand Down Expand Up @@ -69,9 +67,15 @@ impl Wasm {
}))
}

#[wasm_bindgen(js_name = saveRegistrationOptions)]
pub fn save_registration_options() -> JsValue {
serde_wasm_bindgen::to_value(&save_registration_options()).unwrap()
}

#[wasm_bindgen(js_name = updateConfig)]
pub fn update_config(&self, source: &str) {
todo!()
pub fn update_config(&mut self, source: &str) {
*self.0.linter.config_mut() = FluffConfig::from_source(source);
self.0.recheck_files();
}

#[wasm_bindgen(js_name = onInitialize)]
Expand Down Expand Up @@ -230,37 +234,15 @@ fn main_loop(connection: Connection, _init_param: InitializeParams) {
sender.send(Message::Notification(notification)).unwrap();
});

let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
include_text: false.into(),
text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
document_selector: Some(vec![
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/.sqlfluff".into()),
},
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/.sqruff".into()),
},
]),
},
};

let request = Request::new(
"textDocument-didSave".to_owned().into(),
"client/registerCapability".to_owned(),
lsp_types::RegistrationParams {
registrations: vec![Registration {
id: "textDocument/didSave".into(),
method: "textDocument/didSave".into(),
register_options: serde_json::to_value(save_registration_options).unwrap().into(),
}],
},
);

connection.sender.send(Message::Request(request)).unwrap();
let params = save_registration_options();
connection
.sender
.send(Message::Request(Request::new(
"textDocument-didSave".to_owned().into(),
"client/registerCapability".to_owned(),
params,
)))
.unwrap();

for message in &connection.receiver {
match message {
Expand All @@ -282,6 +264,34 @@ fn main_loop(connection: Connection, _init_param: InitializeParams) {
}
}

pub fn save_registration_options() -> lsp_types::RegistrationParams {
let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
include_text: false.into(),
text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
document_selector: Some(vec![
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/.sqlfluff".into()),
},
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/.sqruff".into()),
},
]),
},
};

lsp_types::RegistrationParams {
registrations: vec![Registration {
id: "textDocument/didSave".into(),
method: "textDocument/didSave".into(),
register_options: serde_json::to_value(save_registration_options).unwrap().into(),
}],
}
}

fn new_notification<T>(params: T::Params) -> lsp_server::Notification
where
T: lsp_types::notification::Notification,
Expand Down
21 changes: 20 additions & 1 deletion editors/code/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@ export function activate(context: vscode.ExtensionContext) {
const cl = new LanguageClient(
"sqruff-lsp",
"Sqruff LSP",
{ documentSelector: [{ language: "sql" }] },
{
documentSelector: [{ language: "sql" }],
},
worker,
);

const fileEvents = [
vscode.workspace.createFileSystemWatcher("**/.sqruff"),
vscode.workspace.createFileSystemWatcher("**/.sqlfluff"),
];

for (const fileEvent of fileEvents) {
fileEvent.onDidChange((_) => {
cl.sendRequest("changeConfig");
});
fileEvent.onDidCreate((_) => {
cl.sendRequest("changeConfig");
});
fileEvent.onDidDelete((_) => {
cl.sendRequest("deleteConfig");
});
}

cl.onRequest("loadConfig", async (_path: string) => {
if (vscode.workspace.workspaceFolders === undefined) {
return "";
Expand Down
26 changes: 21 additions & 5 deletions editors/code/src/lsp-worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sqruffInit, * as sqruffLsp from "../dist/lsp";
import sqruffWasmData from "../dist/lsp_bg.wasm";
import * as vscode from "vscode";

import {
createConnection,
Expand All @@ -17,6 +16,16 @@ sqruffInit(sqruffWasmData).then(() => {

const connection = createConnection(reader, writer);

const updateConfig = () => {
loadConfig()
.then((config) => {
lsp.updateConfig(config);
})
.catch((e) => {
console.log(e);
});
};

async function loadConfig(): Promise<string> {
return await connection.sendRequest("loadConfig");
}
Expand All @@ -27,19 +36,26 @@ sqruffInit(sqruffWasmData).then(() => {
let lsp = new sqruffLsp.Wasm(sendDiagnosticsCallback);

connection.onInitialize(() => lsp.onInitialize());
connection.onInitialized(() => updateConfig());

connection.onRequest(
"textDocument/formatting",
(params: DocumentFormattingParams) => {
return lsp.format(params.textDocument.uri);
},
);
connection.onNotification((...args) => {
loadConfig().then((config) => {

});
connection.onRequest("changeConfig", () => {
updateConfig();
});
connection.onRequest("deleteConfig", () => {
lsp.updateConfig("");
});

connection.onNotification((...args) => {
lsp.onNotification(...args);
});
connection.listen();

connection.listen();
self.postMessage("OK");
});

0 comments on commit 6d0c97b

Please sign in to comment.