aboutsummaryrefslogtreecommitdiff
path: root/crates/libanalysis/src/api.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-29 16:03:14 +0100
committerAleksey Kladov <[email protected]>2018-08-29 16:03:14 +0100
commit8abf5363433e977c5393bb569e2a5d559cb0a602 (patch)
tree8bb7bc3097cb9e22af9e3be8605cb4745c2fae5f /crates/libanalysis/src/api.rs
parent2007ccfcfe0bf01c934589dd3c87fda83b06b272 (diff)
Grand refactoring
Diffstat (limited to 'crates/libanalysis/src/api.rs')
-rw-r--r--crates/libanalysis/src/api.rs136
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 @@
1use relative_path::RelativePathBuf;
2use libsyntax2::{File, TextRange, TextUnit, AtomEdit};
3use libeditor;
4use {World, FileId, Query};
5
6pub use libeditor::{
7 LocalEdit, StructureNode, LineIndex, FileSymbol,
8 Runnable, RunnableKind, HighlightedRange, CompletionItem
9};
10
11#[derive(Clone, Debug)]
12pub struct Analysis {
13 pub(crate) imp: World
14}
15
16impl 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)]
84pub 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)]
92pub struct Position {
93 pub file_id: FileId,
94 pub offset: TextUnit,
95}
96
97#[derive(Debug)]
98pub struct SourceFileEdit {
99 pub file_id: FileId,
100 pub edits: Vec<AtomEdit>,
101}
102
103#[derive(Debug)]
104pub 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)]
116pub struct Diagnostic {
117 pub message: String,
118 pub range: TextRange,
119 pub fix: Option<SourceChange>,
120}
121
122impl 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}