Skip to content

Commit 9f3e561

Browse files
use conv_with instead of file_pos and range
1 parent 5661303 commit 9f3e561

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

wasm-demo/src/lib.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#![cfg(target_arch = "wasm32")]
22
#![allow(non_snake_case)]
33

4-
use ra_ide_api::{
5-
Analysis, CompletionItemKind, FileId, FilePosition, InsertTextFormat, LineCol, Severity,
6-
};
7-
use ra_syntax::{SyntaxKind, TextRange};
4+
use ra_ide_api::{Analysis, CompletionItemKind, FileId, FilePosition, InsertTextFormat, Severity};
5+
use ra_syntax::SyntaxKind;
86
use wasm_bindgen::prelude::*;
97

108
mod conv;
@@ -39,12 +37,14 @@ impl WorldState {
3937
self.analysis = analysis;
4038
self.file_id = file_id;
4139

40+
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
41+
4242
let highlights: Vec<_> = self
4343
.analysis
4444
.highlight(file_id)
4545
.unwrap()
4646
.into_iter()
47-
.map(|hl| Highlight { tag: Some(hl.tag), range: self.range(hl.range) })
47+
.map(|hl| Highlight { tag: Some(hl.tag), range: hl.range.conv_with(&line_index) })
4848
.collect();
4949

5050
let diagnostics: Vec<_> = self
@@ -54,7 +54,7 @@ impl WorldState {
5454
.into_iter()
5555
.map(|d| {
5656
let Range { startLineNumber, startColumn, endLineNumber, endColumn } =
57-
self.range(d.range);
57+
d.range.conv_with(&line_index);
5858
Diagnostic {
5959
message: d.message,
6060
severity: match d.severity {
@@ -72,31 +72,11 @@ impl WorldState {
7272
serde_wasm_bindgen::to_value(&UpdateResult { diagnostics, highlights }).unwrap()
7373
}
7474

75-
fn file_pos(&self, line: u32, col_utf16: u32) -> FilePosition {
76-
// monaco doesn't work zero-based
77-
let line_col = LineCol { line: line - 1, col_utf16: col_utf16 - 1 };
78-
let offset = self.analysis.file_line_index(self.file_id).unwrap().offset(line_col);
79-
FilePosition { file_id: self.file_id, offset }
80-
}
81-
82-
fn range(&self, text_range: TextRange) -> Range {
83-
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
84-
let start = line_index.line_col(text_range.start());
85-
let end = line_index.line_col(text_range.end());
86-
87-
Range {
88-
startLineNumber: start.line + 1,
89-
startColumn: start.col_utf16 + 1,
90-
endLineNumber: end.line + 1,
91-
endColumn: end.col_utf16 + 1,
92-
}
93-
}
94-
9575
pub fn completions(&self, line_number: u32, column: u32) -> JsValue {
9676
log::warn!("completions");
9777
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
98-
let pos = Position { line_number, column }.conv_with((&line_index, self.file_id));
9978

79+
let pos = Position { line_number, column }.conv_with((&line_index, self.file_id));
10080
let res = match self.analysis.completions(pos).unwrap() {
10181
Some(items) => items,
10282
None => return JsValue::NULL,
@@ -109,7 +89,7 @@ impl WorldState {
10989
.map(|item| CompletionItem {
11090
kind: item.kind().unwrap_or(CompletionItemKind::Struct).conv(),
11191
label: item.label().to_string(),
112-
range: self.range(item.source_range()),
92+
range: item.source_range().conv_with(&line_index),
11393
detail: item.detail().map(|it| it.to_string()),
11494
insertText: item.text_edit().as_atoms()[0].insert.clone(),
11595
insertTextRules: match item.insert_text_format() {
@@ -126,22 +106,27 @@ impl WorldState {
126106
}
127107

128108
pub fn hover(&self, line_number: u32, column: u32) -> JsValue {
129-
let pos = self.file_pos(line_number, column);
130109
log::warn!("hover");
110+
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
111+
112+
let pos = Position { line_number, column }.conv_with((&line_index, self.file_id));
131113
let info = match self.analysis.hover(pos).unwrap() {
132114
Some(info) => info,
133115
_ => return JsValue::NULL,
134116
};
135117

136118
let value = info.info.to_markup();
137-
let hover =
138-
Hover { contents: vec![MarkdownString { value }], range: self.range(info.range) };
119+
let hover = Hover {
120+
contents: vec![MarkdownString { value }],
121+
range: info.range.conv_with(&line_index),
122+
};
139123

140124
serde_wasm_bindgen::to_value(&hover).unwrap()
141125
}
142126

143127
pub fn code_lenses(&self) -> JsValue {
144128
log::warn!("code_lenses");
129+
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
145130

146131
let results: Vec<_> = self
147132
.analysis
@@ -167,11 +152,11 @@ impl WorldState {
167152
.info
168153
.iter()
169154
.map(|target| target.focus_range().unwrap_or(target.full_range()))
170-
.map(|range| self.range(range))
155+
.map(|range| range.conv_with(&line_index))
171156
.collect();
172157

173158
Some(CodeLensSymbol {
174-
range: self.range(it.node_range),
159+
range: it.node_range.conv_with(&line_index),
175160
command: Some(Command {
176161
id: "editor.action.showReferences".into(),
177162
title,
@@ -185,36 +170,44 @@ impl WorldState {
185170
}
186171

187172
pub fn references(&self, line_number: u32, column: u32) -> JsValue {
188-
let pos = self.file_pos(line_number, column);
189173
log::warn!("references");
174+
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
175+
176+
let pos = Position { line_number, column }.conv_with((&line_index, self.file_id));
190177
let info = match self.analysis.find_all_refs(pos).unwrap() {
191178
Some(info) => info,
192179
_ => return JsValue::NULL,
193180
};
194181

195-
let res: Vec<_> =
196-
info.into_iter().map(|r| Highlight { tag: None, range: self.range(r.range) }).collect();
182+
let res: Vec<_> = info
183+
.into_iter()
184+
.map(|r| Highlight { tag: None, range: r.range.conv_with(&line_index) })
185+
.collect();
197186
serde_wasm_bindgen::to_value(&res).unwrap()
198187
}
199188

200189
pub fn prepare_rename(&self, line_number: u32, column: u32) -> JsValue {
201-
let pos = self.file_pos(line_number, column);
202190
log::warn!("prepare_rename");
191+
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
192+
193+
let pos = Position { line_number, column }.conv_with((&line_index, self.file_id));
203194
let refs = match self.analysis.find_all_refs(pos).unwrap() {
204195
None => return JsValue::NULL,
205196
Some(refs) => refs,
206197
};
207198

208199
let declaration = refs.declaration();
209-
let range = self.range(declaration.range());
200+
let range = declaration.range().conv_with(&line_index);
210201
let text = declaration.name().to_string();
211202

212203
serde_wasm_bindgen::to_value(&RenameLocation { range, text }).unwrap()
213204
}
214205

215206
pub fn rename(&self, line_number: u32, column: u32, new_name: &str) -> JsValue {
216-
let pos = self.file_pos(line_number, column);
217207
log::warn!("rename");
208+
let line_index = self.analysis.file_line_index(self.file_id).unwrap();
209+
210+
let pos = Position { line_number, column }.conv_with((&line_index, self.file_id));
218211
let change = match self.analysis.rename(pos, new_name) {
219212
Ok(Some(change)) => change,
220213
_ => return JsValue::NULL,
@@ -225,7 +218,10 @@ impl WorldState {
225218
.source_file_edits
226219
.iter()
227220
.flat_map(|sfe| sfe.edit.as_atoms())
228-
.map(|atom| TextEdit { range: self.range(atom.delete), text: atom.insert.clone() })
221+
.map(|atom| TextEdit {
222+
range: atom.delete.conv_with(&line_index),
223+
text: atom.insert.clone(),
224+
})
229225
.collect();
230226

231227
serde_wasm_bindgen::to_value(&result).unwrap()

0 commit comments

Comments
 (0)