pub(crate) struct AssistContext<'a, 'db> {
pub(crate) config: &'a AssistConfig,
pub(crate) sema: Semantics<'db, RootDatabase>,
frange: FileRange,
trimmed_range: TextRange,
source_file: SourceFile,
token_at_offset: TokenAtOffset<SyntaxToken>,
covering_element: SyntaxElement,
}Expand description
AssistContext allows to apply an assist or check if it could be applied.
Assists use a somewhat over-engineered approach, given the current needs. The assists workflow consists of two phases. In the first phase, a user asks for the list of available assists. In the second phase, the user picks a particular assist and it gets applied.
There are two peculiarities here:
- first, we ideally avoid computing more things then necessary to answer “is assist applicable” in the first phase.
- second, when we are applying assist, we don’t have a guarantee that there weren’t any changes between the point when user asked for assists and when they applied a particular assist. So, when applying assist, we need to do all the checks from scratch.
To avoid repeating the same code twice for both “check” and “apply”
functions, we use an approach reminiscent of that of Django’s function based
views dealing with forms. Each assist receives a runtime parameter,
resolve. It first check if an edit is applicable (potentially computing
info required to compute the actual edit). If it is applicable, and
resolve is true, it then computes the actual edit.
So, to implement the original assists workflow, we can first apply each edit
with resolve = false, and then applying the selected edit again, with
resolve = true this time.
Note, however, that we don’t actually use such two-phase logic at the moment, because the LSP API is pretty awkward in this place, and it’s much easier to just compute the edit eagerly :-)
Fields§
§config: &'a AssistConfig§sema: Semantics<'db, RootDatabase>§frange: FileRange§trimmed_range: TextRange§source_file: SourceFile§token_at_offset: TokenAtOffset<SyntaxToken>§covering_element: SyntaxElementImplementations§
Source§impl<'a, 'db> AssistContext<'a, 'db>
impl<'a, 'db> AssistContext<'a, 'db>
pub(crate) fn new( sema: Semantics<'db, RootDatabase>, config: &'a AssistConfig, frange: FileRange, ) -> AssistContext<'a, 'db>
pub(crate) fn db(&self) -> &'db RootDatabase
pub(crate) fn offset(&self) -> TextSize
pub(crate) fn vfs_file_id(&self) -> FileId
pub(crate) fn file_id(&self) -> EditionedFileId
pub(crate) fn edition(&self) -> Edition
pub(crate) fn has_empty_selection(&self) -> bool
Sourcepub(crate) fn selection_trimmed(&self) -> TextRange
pub(crate) fn selection_trimmed(&self) -> TextRange
Returns the selected range trimmed for whitespace tokens, that is the range will be snapped to the nearest enclosed token.
pub(crate) fn source_file(&self) -> &SourceFile
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken>
pub(crate) fn find_token_syntax_at_offset( &self, kind: SyntaxKind, ) -> Option<SyntaxToken>
pub(crate) fn find_token_at_offset<T: AstToken>(&self) -> Option<T>
pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N>
pub(crate) fn find_node_at_trimmed_offset<N: AstNode>(&self) -> Option<N>
pub(crate) fn find_node_at_range<N: AstNode>(&self) -> Option<N>
pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N>
Sourcepub(crate) fn covering_element(&self) -> SyntaxElement
pub(crate) fn covering_element(&self) -> SyntaxElement
Returns the element covered by the selection range, this excludes trailing whitespace in the selection.
Auto Trait Implementations§
impl<'a, 'db> !Freeze for AssistContext<'a, 'db>
impl<'a, 'db> !RefUnwindSafe for AssistContext<'a, 'db>
impl<'a, 'db> !Send for AssistContext<'a, 'db>
impl<'a, 'db> !Sync for AssistContext<'a, 'db>
impl<'a, 'db> Unpin for AssistContext<'a, 'db>
impl<'a, 'db> UnsafeUnpin for AssistContext<'a, 'db>
impl<'a, 'db> !UnwindSafe for AssistContext<'a, 'db>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, R> CollectAndApply<T, R> for T
impl<T, R> CollectAndApply<T, R> for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more