-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathlib.rs
More file actions
183 lines (155 loc) · 5.45 KB
/
lib.rs
File metadata and controls
183 lines (155 loc) · 5.45 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer};
pub struct Backend {
name: String,
version: String,
open_files: Arc<Mutex<HashMap<String, String>>>,
client: Option<Client>,
}
impl Backend {
pub fn new(client: Client) -> Self {
Self {
name: "PHPantomLSP".to_string(),
version: "0.1.0".to_string(),
open_files: Arc::new(Mutex::new(HashMap::new())),
client: Some(client),
}
}
pub fn new_test() -> Self {
Self {
name: "PHPantomLSP".to_string(),
version: "0.1.0".to_string(),
open_files: Arc::new(Mutex::new(HashMap::new())),
client: None,
}
}
fn get_word_at_position(&self, content: &str, position: Position) -> Option<String> {
let lines: Vec<&str> = content.lines().collect();
if position.line as usize >= lines.len() {
return None;
}
let line = lines[position.line as usize];
let chars: Vec<char> = line.chars().collect();
if position.character as usize > chars.len() {
return None;
}
let pos = position.character as usize;
// Find word boundaries
let mut start = pos;
let mut end = pos;
// Move start backward to word boundary
while start > 0 && chars[start - 1].is_alphanumeric() {
start -= 1;
}
// Move end forward to word boundary
while end < chars.len() && chars[end].is_alphanumeric() {
end += 1;
}
if start < end {
Some(chars[start..end].iter().collect())
} else {
None
}
}
async fn log(&self, typ: MessageType, message: String) {
if let Some(client) = &self.client {
client.log_message(typ, message).await;
}
}
}
#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult {
capabilities: ServerCapabilities {
hover_provider: Some(HoverProviderCapability::Simple(true)),
completion_provider: Some(CompletionOptions {
resolve_provider: Some(false),
trigger_characters: None,
all_commit_characters: None,
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
}),
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
..ServerCapabilities::default()
},
server_info: Some(ServerInfo {
name: self.name.clone(),
version: Some(self.version.clone()),
}),
})
}
async fn initialized(&self, _: InitializedParams) {
self.log(MessageType::INFO, "PHPantomLSP initialized!".to_string())
.await;
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let doc = params.text_document;
let uri = doc.uri.to_string();
let text = doc.text;
if let Ok(mut files) = self.open_files.lock() {
files.insert(uri.clone(), text);
}
self.log(MessageType::INFO, format!("Opened file: {}", uri))
.await;
}
async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.to_string();
if let Some(change) = params.content_changes.first()
&& let Ok(mut files) = self.open_files.lock()
{
files.insert(uri, change.text.clone());
}
}
async fn did_close(&self, params: DidCloseTextDocumentParams) {
let uri = params.text_document.uri.to_string();
if let Ok(mut files) = self.open_files.lock() {
files.remove(&uri);
}
self.log(MessageType::INFO, format!("Closed file: {}", uri))
.await;
}
async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
let uri = params
.text_document_position_params
.text_document
.uri
.to_string();
let position = params.text_document_position_params.position;
let content = if let Ok(files) = self.open_files.lock() {
files.get(&uri).cloned()
} else {
None
};
if let Some(content) = content
&& let Some(word) = self.get_word_at_position(&content, position)
&& word == "PHPantom"
{
return Ok(Some(Hover {
contents: HoverContents::Scalar(MarkedString::String(
"Welcome to PHPantomLSP!".to_string(),
)),
range: None,
}));
}
Ok(None)
}
async fn completion(&self, _params: CompletionParams) -> Result<Option<CompletionResponse>> {
Ok(Some(CompletionResponse::Array(vec![CompletionItem {
label: "PHPantomLSP".to_string(),
kind: Some(CompletionItemKind::TEXT),
detail: Some("PHPantomLSP completion".to_string()),
insert_text: Some("PHPantomLSP".to_string()),
..CompletionItem::default()
}])))
}
}