diff options
Diffstat (limited to 'crates/libanalysis/src/api.rs')
-rw-r--r-- | crates/libanalysis/src/api.rs | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/crates/libanalysis/src/api.rs b/crates/libanalysis/src/api.rs new file mode 100644 index 000000000..bb4fee398 --- /dev/null +++ b/crates/libanalysis/src/api.rs | |||
@@ -0,0 +1,136 @@ | |||
1 | use relative_path::RelativePathBuf; | ||
2 | use libsyntax2::{File, TextRange, TextUnit, AtomEdit}; | ||
3 | use libeditor; | ||
4 | use {World, FileId, Query}; | ||
5 | |||
6 | pub use libeditor::{ | ||
7 | LocalEdit, StructureNode, LineIndex, FileSymbol, | ||
8 | Runnable, RunnableKind, HighlightedRange, CompletionItem | ||
9 | }; | ||
10 | |||
11 | #[derive(Clone, Debug)] | ||
12 | pub struct Analysis { | ||
13 | pub(crate) imp: World | ||
14 | } | ||
15 | |||
16 | impl Analysis { | ||
17 | pub fn file_syntax(&self, file_id: FileId) -> File { | ||
18 | self.imp.file_syntax(file_id) | ||
19 | .unwrap() | ||
20 | } | ||
21 | pub fn file_line_index(&self, file_id: FileId) -> LineIndex { | ||
22 | self.imp.file_line_index(file_id) | ||
23 | .unwrap() | ||
24 | } | ||
25 | pub fn extend_selection(&self, file: &File, range: TextRange) -> TextRange { | ||
26 | libeditor::extend_selection(file, range).unwrap_or(range) | ||
27 | } | ||
28 | pub fn matching_brace(&self, file: &File, offset: TextUnit) -> Option<TextUnit> { | ||
29 | libeditor::matching_brace(file, offset) | ||
30 | } | ||
31 | pub fn syntax_tree(&self, file_id: FileId) -> String { | ||
32 | let file = self.file_syntax(file_id); | ||
33 | libeditor::syntax_tree(&file) | ||
34 | } | ||
35 | pub fn join_lines(&self, file_id: FileId, range: TextRange) -> SourceChange { | ||
36 | let file = self.file_syntax(file_id); | ||
37 | SourceChange::from_local_edit( | ||
38 | file_id, "join lines", | ||
39 | libeditor::join_lines(&file, range), | ||
40 | ) | ||
41 | } | ||
42 | pub fn on_eq_typed(&self, file_id: FileId, offset: TextUnit) -> Option<SourceChange> { | ||
43 | let file = self.file_syntax(file_id); | ||
44 | Some(SourceChange::from_local_edit( | ||
45 | file_id, "add semicolon", | ||
46 | libeditor::on_eq_typed(&file, offset)?, | ||
47 | )) | ||
48 | } | ||
49 | pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> { | ||
50 | let file = self.file_syntax(file_id); | ||
51 | libeditor::file_structure(&file) | ||
52 | } | ||
53 | pub fn symbol_search(&self, query: Query) -> Vec<(FileId, FileSymbol)> { | ||
54 | self.imp.world_symbols(query) | ||
55 | } | ||
56 | pub fn approximately_resolve_symbol(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, FileSymbol)> { | ||
57 | self.imp.approximately_resolve_symbol(file_id, offset) | ||
58 | .unwrap() | ||
59 | } | ||
60 | pub fn parent_module(&self, file_id: FileId) -> Vec<(FileId, FileSymbol)> { | ||
61 | self.imp.parent_module(file_id) | ||
62 | } | ||
63 | pub fn runnables(&self, file_id: FileId) -> Vec<Runnable> { | ||
64 | let file = self.file_syntax(file_id); | ||
65 | libeditor::runnables(&file) | ||
66 | } | ||
67 | pub fn highlight(&self, file_id: FileId) -> Vec<HighlightedRange> { | ||
68 | let file = self.file_syntax(file_id); | ||
69 | libeditor::highlight(&file) | ||
70 | } | ||
71 | pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Option<Vec<CompletionItem>> { | ||
72 | let file = self.file_syntax(file_id); | ||
73 | libeditor::scope_completion(&file, offset) | ||
74 | } | ||
75 | pub fn assists(&self, file_id: FileId, offset: TextUnit) -> Vec<SourceChange> { | ||
76 | self.imp.assists(file_id, offset) | ||
77 | } | ||
78 | pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> { | ||
79 | self.imp.diagnostics(file_id) | ||
80 | } | ||
81 | } | ||
82 | |||
83 | #[derive(Debug)] | ||
84 | pub struct SourceChange { | ||
85 | pub label: String, | ||
86 | pub source_file_edits: Vec<SourceFileEdit>, | ||
87 | pub file_system_edits: Vec<FileSystemEdit>, | ||
88 | pub cursor_position: Option<Position>, | ||
89 | } | ||
90 | |||
91 | #[derive(Debug)] | ||
92 | pub struct Position { | ||
93 | pub file_id: FileId, | ||
94 | pub offset: TextUnit, | ||
95 | } | ||
96 | |||
97 | #[derive(Debug)] | ||
98 | pub struct SourceFileEdit { | ||
99 | pub file_id: FileId, | ||
100 | pub edits: Vec<AtomEdit>, | ||
101 | } | ||
102 | |||
103 | #[derive(Debug)] | ||
104 | pub enum FileSystemEdit { | ||
105 | CreateFile { | ||
106 | anchor: FileId, | ||
107 | path: RelativePathBuf, | ||
108 | }, | ||
109 | MoveFile { | ||
110 | file: FileId, | ||
111 | path: RelativePathBuf, | ||
112 | } | ||
113 | } | ||
114 | |||
115 | #[derive(Debug)] | ||
116 | pub struct Diagnostic { | ||
117 | pub message: String, | ||
118 | pub range: TextRange, | ||
119 | pub fix: Option<SourceChange>, | ||
120 | } | ||
121 | |||
122 | impl SourceChange { | ||
123 | pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange { | ||
124 | let file_edit = SourceFileEdit { | ||
125 | file_id, | ||
126 | edits: edit.edit.into_atoms(), | ||
127 | }; | ||
128 | SourceChange { | ||
129 | label: label.to_string(), | ||
130 | source_file_edits: vec![file_edit], | ||
131 | file_system_edits: vec![], | ||
132 | cursor_position: edit.cursor_position | ||
133 | .map(|offset| Position { offset, file_id }) | ||
134 | } | ||
135 | } | ||
136 | } | ||