forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LSP: generate doc comments for trait functions (FuelLabs#4665)
## Description Closes FuelLabs#4645 This PR lets the user generate doc comments for ABI methods. Previously it was working for functions and function implementations of ABI/trait, but not on the ABI/trait definition itself. Since the output is exactly the same for FunctionDecl to TraitFn, I wanted to do this in a way where they could share the code. I ended up writing a trait called `FunctionSignature` that both types implement that exposes `parameters()` and `return_type()`. This allows me to use the same code for code actions and I suspect it will come in handy in the future. One problem was that `FunctionDecl`'s return_type was `TypeArgument` while `TraitFn` had its return type as `TypeId` with an additional `return_type_span` field. I changed it so that `TraitFn`'s return_type is now `TypeArgument` and removed `return_type_span`. I also needed to add a `span` field to `TraitFn` since previously its `span()` was returning only the span of the function name, not the whole function. From what I could see, this doesn't impact the spans of any existing errors. ### Testing I added an integration test to the LSP for this scenario. ### Example ![Jun-13-2023 14-40-21](https://github.com/FuelLabs/sway/assets/47993817/5ba750b6-9f9d-4cb9-96a8-c791bf93b77d) ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
30 changed files
with
250 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
sway-lsp/src/capabilities/code_actions/common/basic_doc_comment.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::capabilities::code_actions::{CodeAction, CodeActionContext, CODE_ACTION_DOC_TITLE}; | ||
use lsp_types::{Range, Url}; | ||
use sway_types::Spanned; | ||
|
||
use super::generate_doc::GenerateDocCodeAction; | ||
|
||
pub struct BasicDocCommentCodeAction<'a, T: Spanned> { | ||
decl: &'a T, | ||
uri: &'a Url, | ||
} | ||
|
||
impl<'a, T: Spanned> GenerateDocCodeAction<'a, T> for BasicDocCommentCodeAction<'a, T> {} | ||
|
||
impl<'a, T: Spanned> CodeAction<'a, T> for BasicDocCommentCodeAction<'a, T> { | ||
fn new(ctx: CodeActionContext<'a>, decl: &'a T) -> Self { | ||
Self { decl, uri: ctx.uri } | ||
} | ||
|
||
fn new_text(&self) -> String { | ||
self.default_template() | ||
} | ||
|
||
fn range(&self) -> Range { | ||
self.range_before() | ||
} | ||
|
||
fn title(&self) -> String { | ||
CODE_ACTION_DOC_TITLE.to_string() | ||
} | ||
|
||
fn decl(&self) -> &T { | ||
self.decl | ||
} | ||
|
||
fn uri(&self) -> &Url { | ||
self.uri | ||
} | ||
} |
Oops, something went wrong.