diff options
author | Aleksey Kladov <[email protected]> | 2020-02-18 17:35:10 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-26 11:55:50 +0000 |
commit | c3a4c4429de83450654795534e64e878a774a088 (patch) | |
tree | 12d89798f61b276f8bd640db07276a7d4e92b1c2 /crates/ra_assists/src/assist_ctx.rs | |
parent | 04deae3dba7c9b7054f7a1d64e4b93a05aecc132 (diff) |
Refactor primary IDE API
This introduces the new type -- Semantics.
Semantics maps SyntaxNodes to various semantic info, such as type,
name resolution or macro expansions.
To do so, Semantics maintains a HashMap which maps every node it saw
to the file from which the node originated. This is enough to get all
the necessary hir bits just from syntax.
Diffstat (limited to 'crates/ra_assists/src/assist_ctx.rs')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 40 |
1 files changed, 11 insertions, 29 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 5aab5fb8b..c25d2e323 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. | 1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. |
2 | use hir::{InFile, SourceAnalyzer, SourceBinder}; | 2 | use hir::Semantics; |
3 | use ra_db::{FileRange, SourceDatabase}; | 3 | use ra_db::FileRange; |
4 | use ra_fmt::{leading_indent, reindent}; | 4 | use ra_fmt::{leading_indent, reindent}; |
5 | use ra_ide_db::RootDatabase; | 5 | use ra_ide_db::RootDatabase; |
6 | use ra_syntax::{ | 6 | use ra_syntax::{ |
@@ -74,29 +74,23 @@ pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>; | |||
74 | /// Note, however, that we don't actually use such two-phase logic at the | 74 | /// Note, however, that we don't actually use such two-phase logic at the |
75 | /// moment, because the LSP API is pretty awkward in this place, and it's much | 75 | /// moment, because the LSP API is pretty awkward in this place, and it's much |
76 | /// easier to just compute the edit eagerly :-) | 76 | /// easier to just compute the edit eagerly :-) |
77 | #[derive(Debug)] | 77 | #[derive(Clone)] |
78 | pub(crate) struct AssistCtx<'a> { | 78 | pub(crate) struct AssistCtx<'a> { |
79 | pub(crate) sema: &'a Semantics<'a, RootDatabase>, | ||
79 | pub(crate) db: &'a RootDatabase, | 80 | pub(crate) db: &'a RootDatabase, |
80 | pub(crate) frange: FileRange, | 81 | pub(crate) frange: FileRange, |
81 | source_file: SourceFile, | 82 | source_file: SourceFile, |
82 | should_compute_edit: bool, | 83 | should_compute_edit: bool, |
83 | } | 84 | } |
84 | 85 | ||
85 | impl Clone for AssistCtx<'_> { | ||
86 | fn clone(&self) -> Self { | ||
87 | AssistCtx { | ||
88 | db: self.db, | ||
89 | frange: self.frange, | ||
90 | source_file: self.source_file.clone(), | ||
91 | should_compute_edit: self.should_compute_edit, | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | |||
96 | impl<'a> AssistCtx<'a> { | 86 | impl<'a> AssistCtx<'a> { |
97 | pub fn new(db: &RootDatabase, frange: FileRange, should_compute_edit: bool) -> AssistCtx { | 87 | pub fn new( |
98 | let parse = db.parse(frange.file_id); | 88 | sema: &'a Semantics<'a, RootDatabase>, |
99 | AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit } | 89 | frange: FileRange, |
90 | should_compute_edit: bool, | ||
91 | ) -> AssistCtx<'a> { | ||
92 | let source_file = sema.parse(frange.file_id); | ||
93 | AssistCtx { sema, db: sema.db, frange, source_file, should_compute_edit } | ||
100 | } | 94 | } |
101 | 95 | ||
102 | pub(crate) fn add_assist( | 96 | pub(crate) fn add_assist( |
@@ -138,18 +132,6 @@ impl<'a> AssistCtx<'a> { | |||
138 | pub(crate) fn covering_element(&self) -> SyntaxElement { | 132 | pub(crate) fn covering_element(&self) -> SyntaxElement { |
139 | find_covering_element(self.source_file.syntax(), self.frange.range) | 133 | find_covering_element(self.source_file.syntax(), self.frange.range) |
140 | } | 134 | } |
141 | pub(crate) fn source_binder(&self) -> SourceBinder<'a, RootDatabase> { | ||
142 | SourceBinder::new(self.db) | ||
143 | } | ||
144 | pub(crate) fn source_analyzer( | ||
145 | &self, | ||
146 | node: &SyntaxNode, | ||
147 | offset: Option<TextUnit>, | ||
148 | ) -> SourceAnalyzer { | ||
149 | let src = InFile::new(self.frange.file_id.into(), node); | ||
150 | self.source_binder().analyze(src, offset) | ||
151 | } | ||
152 | |||
153 | pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement { | 135 | pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement { |
154 | find_covering_element(self.source_file.syntax(), range) | 136 | find_covering_element(self.source_file.syntax(), range) |
155 | } | 137 | } |