Skip to content

Commit 7cefa33

Browse files
committed
feat: made LSP work
1 parent 07ffc58 commit 7cefa33

5 files changed

Lines changed: 43 additions & 18 deletions

File tree

compiler/diagnostics/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ pub fn dump_diagnostics() {
7070
})
7171
}
7272

73+
pub fn clear_diagnostics() {
74+
DIAGNOSTIC_CONTAINER.with_borrow_mut(|f| f.diagnostics.clear())
75+
}
76+
7377
pub fn has_diagnostics() -> bool {
7478
DIAGNOSTIC_CONTAINER.with_borrow(|f| return !f.diagnostics.is_empty())
7579
}

compiler/lexer/src/lexer.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ const USE_KEYWORD_HASH: u64 = hash!("use");
3939
/// ```
4040
/// let result: LexerParseResult<Vec<LexerToken>> = lexer_parse_file("test_file.qf").expect("Lexer didn't work");
4141
/// ```
42-
pub fn lexer_parse_file(file_path: &String) -> DiagnosticResult<Vec<LexerToken>> {
43-
let contents: String = match fs::read_to_string(file_path) {
42+
pub fn lexer_parse_file(path: &String) -> DiagnosticResult<Vec<LexerToken>> {
43+
let contents: String = match fs::read_to_string(path) {
4444
Ok(v) => v,
45-
Err(_) => panic!("Couldn't read the file {}", file_path),
45+
Err(_) => panic!("Couldn't read the file {}", path),
4646
};
4747

48+
lexer_parse(contents, path)
49+
}
50+
51+
pub fn lexer_parse(contents: String, file_path: &String) -> DiagnosticResult<Vec<LexerToken>> {
4852
let mut tokens: Vec<LexerToken> = Vec::new();
4953

5054
let mut i: usize = 0;

lsp/src/bridge.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use ast_parser::parse_ast_ctx;
22
use astoir::run_astoir_mir;
3-
use diagnostics::{diagnostic::Diagnostic, get_diagnostics, has_diagnostics};
4-
use lexer::lexer::lexer_parse_file;
3+
use diagnostics::{clear_diagnostics, diagnostic::Diagnostic, get_diagnostics, has_diagnostics};
4+
use lexer::lexer::lexer_parse;
55

6-
pub fn check_for_file(url: String) -> Vec<Diagnostic> {
7-
let lexer = lexer_parse_file(&url);
6+
pub fn check_for_file(url: String, text: String) -> Vec<Diagnostic> {
7+
clear_diagnostics();
8+
9+
let lexer = lexer_parse(text, &url);
810
if has_diagnostics() {
911
return get_diagnostics();
1012
}
@@ -14,7 +16,7 @@ pub fn check_for_file(url: String) -> Vec<Diagnostic> {
1416
return get_diagnostics();
1517
}
1618

17-
run_astoir_mir(ast.unwrap());
19+
let _ = run_astoir_mir(ast.unwrap());
1820

1921
return get_diagnostics();
2022
}

lsp/src/diags.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use tower_lsp::lsp_types::{
33
Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Location, Position, Range, Url,
44
};
55

6-
use crate::main;
7-
86
pub fn to_tower_diag(diagnostic: diagnostics::diagnostic::Diagnostic) -> Diagnostic {
97
let severity = match diagnostic.level {
108
Level::Error => DiagnosticSeverity::ERROR,

lsp/src/lsp.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::{
2-
collections::{HashMap, HashSet},
2+
collections::HashSet,
33
sync::{Arc, Mutex},
44
};
55

66
use tower_lsp::{
77
Client, LanguageServer,
88
lsp_types::{
9-
CompletionOptions, DidOpenTextDocumentParams, HoverProviderCapability, InitializeParams,
10-
InitializeResult, InitializedParams, MessageType, ServerCapabilities, ServerInfo,
11-
TextDocumentSyncCapability, TextDocumentSyncKind,
9+
DidChangeTextDocumentParams, DidOpenTextDocumentParams, InitializeParams, InitializeResult,
10+
MessageType, ServerCapabilities, ServerInfo, TextDocumentSyncCapability,
11+
TextDocumentSyncKind,
1212
},
1313
};
1414

@@ -28,8 +28,8 @@ impl LanguageServer for LSPBackend {
2828
) -> tower_lsp::jsonrpc::Result<InitializeResult> {
2929
Ok(InitializeResult {
3030
capabilities: ServerCapabilities {
31-
hover_provider: Some(HoverProviderCapability::Simple(true)),
32-
completion_provider: Some(CompletionOptions::default()),
31+
hover_provider: None,
32+
completion_provider: None,
3333
text_document_sync: Some(TextDocumentSyncCapability::Kind(
3434
TextDocumentSyncKind::FULL,
3535
)),
@@ -50,14 +50,31 @@ impl LanguageServer for LSPBackend {
5050

5151
let mut diags = vec![];
5252

53+
for diag in check_for_file(uri.clone().replace("file://", ""), text.clone()) {
54+
diags.push(to_tower_diag(diag))
55+
}
56+
57+
self.lsp_client.log_message(MessageType::ERROR, text).await;
58+
5359
self.lsp_client
54-
.log_message(MessageType::ERROR, format!("Path: {}", uri))
60+
.publish_diagnostics(uri.parse().unwrap(), diags, None)
5561
.await;
62+
}
5663

57-
for diag in check_for_file(uri.clone().replace("file://", "")) {
64+
async fn did_change(&self, params: DidChangeTextDocumentParams) {
65+
let uri = params.text_document.uri.to_string();
66+
let text = params.content_changes[0].text.clone();
67+
68+
self.documents.lock().unwrap().insert(uri.clone());
69+
70+
let mut diags = vec![];
71+
72+
for diag in check_for_file(uri.clone().replace("file://", ""), text.clone()) {
5873
diags.push(to_tower_diag(diag))
5974
}
6075

76+
self.lsp_client.log_message(MessageType::ERROR, text).await;
77+
6178
self.lsp_client
6279
.publish_diagnostics(uri.parse().unwrap(), diags, None)
6380
.await;

0 commit comments

Comments
 (0)