-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.rs
130 lines (114 loc) · 4.12 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Deny usage of print and eprint as it won't have same result
// in WASI as if doing in standard program, you must really know
// what you are doing to disable that lint (and you don't know)
#![deny(clippy::print_stdout)]
#![deny(clippy::print_stderr)]
use anyhow::Result;
use lapce_plugin::{
psp_types::{
lsp_types::{request::Initialize, DocumentFilter, DocumentSelector, InitializeParams, Url, MessageType},
Request,
},
register_plugin, LapcePlugin, VoltEnvironment, PLUGIN_RPC,
};
use serde_json::Value;
#[derive(Default)]
struct State {}
register_plugin!(State);
fn initialize(params: InitializeParams) -> Result<()> {
let document_selector: DocumentSelector = vec![DocumentFilter {
// lsp language id
language: Some(String::from("language_id")),
// glob pattern
pattern: Some(String::from("**/*.{ext1,ext2}")),
// like file:
scheme: None,
}];
let mut server_args = vec![];
// Check for user specified LSP server path
// ```
// [lapce-plugin-name.lsp]
// serverPath = "[path or filename]"
// serverArgs = ["--arg1", "--arg2"]
// ```
if let Some(options) = params.initialization_options.as_ref() {
if let Some(lsp) = options.get("lsp") {
if let Some(args) = lsp.get("serverArgs") {
if let Some(args) = args.as_array() {
if !args.is_empty() {
server_args = vec![];
}
for arg in args {
if let Some(arg) = arg.as_str() {
server_args.push(arg.to_string());
}
}
}
}
if let Some(server_path) = lsp.get("serverPath") {
if let Some(server_path) = server_path.as_str() {
if !server_path.is_empty() {
let server_uri = Url::parse(&format!("urn:{}", server_path))?;
PLUGIN_RPC.start_lsp(
server_uri,
server_args,
document_selector,
params.initialization_options,
);
return Ok(());
}
}
}
}
}
// Architecture check
let _ = match VoltEnvironment::architecture().as_deref() {
Ok("x86_64") => "x86_64",
Ok("aarch64") => "aarch64",
_ => return Ok(()),
};
// OS check
let _ = match VoltEnvironment::operating_system().as_deref() {
Ok("macos") => "macos",
Ok("linux") => "linux",
Ok("windows") => "windows",
_ => return Ok(()),
};
// Download URL
// let _ = format!("https://github.com/<name>/<project>/releases/download/<version>/{filename}");
// see lapce_plugin::Http for available API to download files
let _ = match VoltEnvironment::operating_system().as_deref() {
Ok("windows") => {
format!("{}.exe", "[filename]")
}
_ => "[filename]".to_string(),
};
// Plugin working directory
let volt_uri = VoltEnvironment::uri()?;
let server_uri = Url::parse(&volt_uri)?.join("[filename]")?;
// if you want to use server from PATH
// let server_uri = Url::parse(&format!("urn:{filename}"))?;
// Available language IDs
// https://github.com/lapce/lapce/blob/HEAD/lapce-proxy/src/buffer.rs#L173
PLUGIN_RPC.start_lsp(
server_uri,
server_args,
document_selector,
params.initialization_options,
);
Ok(())
}
impl LapcePlugin for State {
fn handle_request(&mut self, _id: u64, method: String, params: Value) {
#[allow(clippy::single_match)]
match method.as_str() {
Initialize::METHOD => {
let params: InitializeParams = serde_json::from_value(params).unwrap();
if let Err(e) = initialize(params) {
PLUGIN_RPC.window_show_message(MessageType::ERROR, format!("plugin returned with error: {e}"))
}
}
_ => {}
}
}
}