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.
Goto links for LSP hover (FuelLabs#4539)
## Description This is a subset of https://github.com/FuelLabs/sway/pull/4532/files. I'm trying to narrow down why some of the e2e tests are failing. This PR contains the changes for the "Go to type" links only, excluding the changes in sway-core that were needed for "Goto implementations" to work. Closes FuelLabs#2852 <img width="673" alt="image" src="https://user-images.githubusercontent.com/47993817/236371214-0b82b509-9a59-4575-b1d1-5acd505da255.png"> ## 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). - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] 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). - [ ] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
15 changed files
with
275 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::{ | ||
core::{session::Session, token::get_range_from_span}, | ||
utils::document::get_url_from_span, | ||
}; | ||
use std::sync::Arc; | ||
use sway_core::{language::CallPath, Engines, TypeId, TypeInfo}; | ||
|
||
use sway_types::{Span, Spanned}; | ||
use tower_lsp::lsp_types::{Range, Url}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RelatedType { | ||
pub name: String, | ||
pub uri: Url, | ||
pub range: Range, | ||
pub callpath: CallPath, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct HoverLinkContents<'a> { | ||
pub related_types: Vec<RelatedType>, | ||
pub implementations: Vec<Span>, | ||
session: Arc<Session>, | ||
engines: Engines<'a>, | ||
} | ||
|
||
impl<'a> HoverLinkContents<'a> { | ||
pub fn new(session: Arc<Session>, engines: Engines<'a>) -> Self { | ||
Self { | ||
related_types: Vec::new(), | ||
implementations: Vec::new(), | ||
session, | ||
engines, | ||
} | ||
} | ||
|
||
/// Adds the given type and any related type parameters to the list of related types. | ||
pub fn add_related_types(&mut self, type_id: &TypeId) { | ||
let type_info = self.engines.te().get(*type_id); | ||
match type_info { | ||
TypeInfo::Enum(decl_ref) => { | ||
let decl = self.engines.de().get_enum(&decl_ref); | ||
self.add_related_type(decl_ref.name().to_string(), &decl.span(), decl.call_path); | ||
decl.type_parameters | ||
.iter() | ||
.for_each(|type_param| self.add_related_types(&type_param.type_id)); | ||
} | ||
TypeInfo::Struct(decl_ref) => { | ||
let decl = self.engines.de().get_struct(&decl_ref); | ||
self.add_related_type(decl_ref.name().to_string(), &decl.span(), decl.call_path); | ||
decl.type_parameters | ||
.iter() | ||
.for_each(|type_param| self.add_related_types(&type_param.type_id)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
/// Adds a single type to the list of related types. | ||
fn add_related_type(&mut self, name: String, span: &Span, callpath: CallPath) { | ||
if let Ok(mut uri) = get_url_from_span(span) { | ||
let converted_url = self.session.sync.temp_to_workspace_url(&uri); | ||
if let Ok(url) = converted_url { | ||
uri = url; | ||
} | ||
let range = get_range_from_span(span); | ||
self.related_types.push(RelatedType { | ||
name, | ||
uri, | ||
range, | ||
callpath, | ||
}); | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::error::DirectoryError; | ||
use std::path::PathBuf; | ||
use sway_types::Span; | ||
use tower_lsp::lsp_types::Url; | ||
|
||
/// Create a [Url] from a [PathBuf]. | ||
pub fn get_url_from_path(path: &PathBuf) -> Result<Url, DirectoryError> { | ||
Url::from_file_path(path).map_err(|_| DirectoryError::UrlFromPathFailed { | ||
path: path.to_string_lossy().to_string(), | ||
}) | ||
} | ||
|
||
/// Create a [Url] from a [Span]. | ||
pub fn get_url_from_span(span: &Span) -> Result<Url, DirectoryError> { | ||
if let Some(path) = span.path() { | ||
get_url_from_path(path) | ||
} else { | ||
Err(DirectoryError::UrlFromSpanFailed { | ||
span: span.as_str().to_string(), | ||
}) | ||
} | ||
} |
Oops, something went wrong.