Skip to content

Commit

Permalink
Create lsp crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Sep 22, 2023
1 parent 4a1771d commit 3627e6c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = ["parser", "cli", "typechecker", "ruff_python_import_resolver"]

members = ["parser", "cli", "typechecker", "ruff_python_import_resolver", "lsp"]
resolver = "2"

[workspace.dependencies]
log = { version = "0.4.17" }
Expand Down
18 changes: 18 additions & 0 deletions lsp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "lsp"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
typechecker = { path = "../typechecker" }
parser = { path = "../parser" }
env_logger = "0.9.0"
serde_json = "1.0.78"
tokio = { version = "1.17.0", features = ["full"] }
tower-lsp = { version = "0.19.0", features = ["proposed"]}
serde = { version = "1.0", features = ["derive"] }
dashmap = "5.1.0"
log = "0.4.14"
im-rc = "15.0.0"
99 changes: 99 additions & 0 deletions lsp/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::path::PathBuf;

use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};

use typechecker::build::BuildManager;
use typechecker::settings::{ImportDiscovery, Settings};

#[derive(Debug)]
struct Backend {
client: Client,
}

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult::default())
}

async fn initialized(&self, _: InitializedParams) {
self.client
.log_message(MessageType::INFO, "server initialized!")
.await;
}

#[allow(unused_variables)]
async fn did_open(&self, params: DidOpenTextDocumentParams) {
self.client
.log_message(MessageType::INFO, "file opened!")
.await;
let uri = params.text_document.uri;
// check if the uri is path buf or just return
let path = uri.to_file_path().unwrap_or_default();
let file_name = path
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default();
let file_name = file_name.to_string();
let file_content = params.text_document.text;
let settings = Settings {
debug: true,
root: path.clone(),
import_discovery: ImportDiscovery {
python_executable: None,
},
};
let mut build_manager = BuildManager::new(vec![], settings);
build_manager.add_source(&path);
build_manager.build();
let errs = build_manager.get_errors();
for err in errs {
self.client
.publish_diagnostics(
uri.clone(),
vec![Diagnostic {
range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 0,
},
},
severity: Some(DiagnosticSeverity::ERROR),
code: None,
code_description: None,
source: Some("typechecker".to_string()),
message: err,
related_information: None,
tags: None,
data: None,
}],
None,
)
.await;
}
}

async fn shutdown(&self) -> Result<()> {
Ok(())
}
}

#[tokio::main]
async fn main() {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();

let build_manager = Box::new(BuildManager::new(vec![], Settings::test_settings()));
let (service, socket) = LspService::new(|client| Backend {
client,
build_manager,
});
Server::new(stdin, stdout, socket).serve(service).await;
}

0 comments on commit 3627e6c

Please sign in to comment.