From 61739b0c177ea96c9ad253e41ee2c1a620d98b47 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Jul 2019 15:43:34 +0300 Subject: Document AssistBuilder closes #1603 --- crates/ra_assists/src/assist_ctx.rs | 15 +++++++++++---- crates/ra_assists/src/auto_import.rs | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index a12c3ed54..c45262efa 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -129,10 +129,13 @@ pub(crate) struct AssistBuilder { } impl AssistBuilder { + /// Replaces specified `range` of text with a given string. pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into) { self.edit.replace(range, replace_with.into()) } + /// Replaces specified `node` of text with a given string, reindenting the + /// string to maintain `node`'s existing indent. pub(crate) fn replace_node_and_indent( &mut self, node: &SyntaxNode, @@ -145,27 +148,31 @@ impl AssistBuilder { self.replace(node.text_range(), replace_with) } - pub(crate) fn set_edit_builder(&mut self, edit: TextEditBuilder) { - self.edit = edit; - } - + /// Remove specified `range` of text. #[allow(unused)] pub(crate) fn delete(&mut self, range: TextRange) { self.edit.delete(range) } + /// Append specified `text` at the given `offset` pub(crate) fn insert(&mut self, offset: TextUnit, text: impl Into) { self.edit.insert(offset, text.into()) } + /// Specify desired position of the cursor after the assist is applied. pub(crate) fn set_cursor(&mut self, offset: TextUnit) { self.cursor_position = Some(offset) } + /// Specify that the assist should be active withing the `target` range. + /// + /// Target ranges are used to sort assists: the smaller the target range, + /// the more specific assist is, and so it should be sorted first. pub(crate) fn target(&mut self, target: TextRange) { self.target = Some(target) } + /// Get access to the raw `TextEditBuilder`. pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder { &mut self.edit } diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 43e75eee1..a32e2f9b6 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -562,9 +562,12 @@ pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option) -> Option