Skip to content

Commit

Permalink
Merge branch 'master' into josh/lsp_testing_framework
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBatty authored Aug 21, 2023
2 parents 97b35a2 + d816923 commit 5f82bac
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 470 deletions.
11 changes: 2 additions & 9 deletions sway-lsp/benches/lsp_benchmarks/token_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ fn benchmarks(c: &mut Criterion) {

c.bench_function("tokens_for_file", |b| {
b.iter(|| {
let _: Vec<_> = session
.token_map()
.tokens_for_file(engines.se(), &uri)
.collect();
let _: Vec<_> = session.token_map().tokens_for_file(&uri).collect();

This comment has been minimized.

Copy link
@cr-fuel

cr-fuel Aug 21, 2023

Contributor

Is this style preferred? I usually write the same statement like this:

 let _ = session.token_map().tokens_for_file(&uri).collect::<Vec<_>>();
})
});

Expand All @@ -32,11 +29,7 @@ fn benchmarks(c: &mut Criterion) {
});

c.bench_function("token_at_position", |b| {
b.iter(|| {
session
.token_map()
.token_at_position(engines.se(), &uri, position)
})
b.iter(|| session.token_map().token_at_position(&uri, position))
});

c.bench_function("parent_decl_at_position", |b| {
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/capabilities/code_actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn code_actions(
let engines = session.engines.read();
let (_, token) = session
.token_map()
.token_at_position(engines.se(), temp_uri, range.start)?;
.token_at_position(temp_uri, range.start)?;

let ctx = CodeActionContext {
engines: &engines,
Expand Down
17 changes: 7 additions & 10 deletions sway-lsp/src/capabilities/completion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::token::TokenIdent;
use lsp_types::{
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, CompletionTextEdit, Position,
Range, TextEdit,
Expand All @@ -7,16 +8,15 @@ use sway_core::{
namespace::Items,
Engines, TypeId, TypeInfo,
};
use sway_types::Ident;

pub(crate) fn to_completion_items(
namespace: &Items,
engines: &Engines,
ident_to_complete: &Ident,
ident_to_complete: &TokenIdent,
fn_decl: &TyFunctionDecl,
position: Position,
) -> Vec<CompletionItem> {
type_id_of_raw_ident(engines, namespace, ident_to_complete, fn_decl)
type_id_of_raw_ident(engines, namespace, &ident_to_complete.name, fn_decl)
.map(|type_id| completion_items_for_type_id(engines, namespace, type_id, position))
.unwrap_or_default()
}
Expand All @@ -30,7 +30,6 @@ fn completion_items_for_type_id(
) -> Vec<CompletionItem> {
let mut completion_items = vec![];
let type_info = engines.te().get(type_id);

if let TypeInfo::Struct(decl_ref) = type_info {
let struct_decl = engines.de().get_struct(&decl_ref.id().clone());
for field in struct_decl.fields {
Expand Down Expand Up @@ -134,18 +133,16 @@ fn replace_self_with_type_str(
fn type_id_of_raw_ident(
engines: &Engines,
namespace: &Items,
ident: &Ident,
ident_name: &str,
fn_decl: &TyFunctionDecl,
) -> Option<TypeId> {
let full_ident = ident.as_str();

// If this ident has no field accesses or chained methods, look for it in the local function scope.
if !full_ident.contains('.') {
return type_id_of_local_ident(full_ident, fn_decl);
if !ident_name.contains('.') {
return type_id_of_local_ident(ident_name, fn_decl);
}

// Otherwise, start with the first part of the ident and follow the subsequent types.
let parts = full_ident.split('.').collect::<Vec<&str>>();
let parts = ident_name.split('.').collect::<Vec<&str>>();
let mut curr_type_id = type_id_of_local_ident(parts[0], fn_decl);
let mut i = 1;

Expand Down
12 changes: 5 additions & 7 deletions sway-lsp/src/capabilities/document_symbol.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::core::token::{get_range_from_span, SymbolKind, Token};
use crate::core::token::{SymbolKind, Token, TokenIdent};
use lsp_types::{self, Location, SymbolInformation, Url};
use sway_types::{Ident, Spanned};

pub fn to_symbol_information<I>(tokens: I, url: Url) -> Vec<SymbolInformation>
where
I: Iterator<Item = (Ident, Token)>,
I: Iterator<Item = (TokenIdent, Token)>,
{
let mut symbols: Vec<SymbolInformation> = vec![];

Expand Down Expand Up @@ -47,12 +46,11 @@ pub(crate) fn symbol_kind(symbol_kind: &SymbolKind) -> lsp_types::SymbolKind {

#[allow(warnings)]
// TODO: the "deprecated: None" field is deprecated according to this library
fn symbol_info(ident: &Ident, token: &Token, url: Url) -> SymbolInformation {
let range = get_range_from_span(&ident.span());
fn symbol_info(ident: &TokenIdent, token: &Token, url: Url) -> SymbolInformation {
SymbolInformation {
name: ident.as_str().to_string(),
name: ident.name.to_string(),
kind: symbol_kind(&token.kind),
location: Location::new(url, range),
location: Location::new(url, ident.range),
tags: None,
container_name: None,
deprecated: None,
Expand Down
33 changes: 14 additions & 19 deletions sway-lsp/src/capabilities/hover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) mod hover_link_contents;
use crate::{
core::{
session::Session,
token::{get_range_from_span, to_ident_key, SymbolKind, Token, TypedAstToken},
token::{SymbolKind, Token, TypedAstToken},
},
utils::{
attributes::doc_comment_attributes, keyword_docs::KeywordDocs, markdown, markup::Markup,
Expand All @@ -16,7 +16,7 @@ use sway_core::{
};

use lsp_types::{self, Position, Url};
use sway_types::{Ident, Span, Spanned};
use sway_types::{Span, Spanned};

use self::hover_link_contents::HoverLinkContents;

Expand All @@ -27,18 +27,15 @@ pub fn hover_data(
url: Url,
position: Position,
) -> Option<lsp_types::Hover> {
let engines = session.engines.read();
let (ident, token) = session
.token_map()
.token_at_position(engines.se(), &url, position)?;
let range = get_range_from_span(&ident.span());
let (ident, token) = session.token_map().token_at_position(&url, position)?;
let range = ident.range;

// check if our token is a keyword
if matches!(
token.kind,
SymbolKind::BoolLiteral | SymbolKind::Keyword | SymbolKind::SelfKeyword
) {
let name = ident.as_str();
let name = &ident.name;
let documentation = keyword_docs.get(name).unwrap();
let prefix = format!("\n```sway\n{name}\n```\n\n---\n\n");
let formatted_doc = format!("{prefix}{documentation}");
Expand All @@ -55,17 +52,17 @@ pub fn hover_data(
Some(decl_ident) => {
let decl_token = session
.token_map()
.try_get(&to_ident_key(&decl_ident))
.try_get(&decl_ident)
.try_unwrap()
.map(|item| item.value().clone())?;
(decl_ident, decl_token)
}
// The `TypeInfo` of the token does not contain an `Ident`. In this case,
// we use the `Ident` of the token itself.
None => (ident, token),
None => (ident.clone(), token),
};

let contents = hover_format(session.clone(), &engines, &decl_token, &decl_ident);
let contents = hover_format(session.clone(), &engines, &decl_token, &decl_ident.name);
Some(lsp_types::Hover {
contents,
range: Some(range),
Expand Down Expand Up @@ -126,11 +123,9 @@ fn hover_format(
session: Arc<Session>,
engines: &Engines,
token: &Token,
ident: &Ident,
ident_name: &str,
) -> lsp_types::HoverContents {
let decl_engine = engines.de();

let token_name: String = ident.as_str().into();
let doc_comment = format_doc_attributes(token);

let format_name_with_type = |name: &str, type_id: &TypeId| -> String {
Expand All @@ -153,7 +148,7 @@ fn hover_format(
Some(format_variable_hover(
var_decl.mutability.is_mutable(),
&type_name,
&token_name,
ident_name,
))
}
ty::TyDecl::StructDecl(ty::StructDecl { decl_id, .. }) => {
Expand All @@ -162,7 +157,7 @@ fn hover_format(
Some(format_visibility_hover(
struct_decl.visibility,
decl.friendly_type_name(),
&token_name,
ident_name,
))
}
ty::TyDecl::TraitDecl(ty::TraitDecl { decl_id, .. }) => {
Expand All @@ -171,7 +166,7 @@ fn hover_format(
Some(format_visibility_hover(
trait_decl.visibility,
decl.friendly_type_name(),
&token_name,
ident_name,
))
}
ty::TyDecl::EnumDecl(ty::EnumDecl { decl_id, .. }) => {
Expand All @@ -180,12 +175,12 @@ fn hover_format(
Some(format_visibility_hover(
enum_decl.visibility,
decl.friendly_type_name(),
&token_name,
ident_name,
))
}
ty::TyDecl::AbiDecl(ty::AbiDecl { .. }) => {
hover_link_contents.add_implementations_for_decl(decl);
Some(format!("{} {}", decl.friendly_type_name(), &token_name))
Some(format!("{} {}", decl.friendly_type_name(), &ident_name))
}
_ => None,
},
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/capabilities/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn inlay_hints(

let hints: Vec<lsp_types::InlayHint> = session
.token_map()
.tokens_for_file(engines.se(), uri)
.tokens_for_file(uri)
.filter_map(|(_, token)| {
token.typed.as_ref().and_then(|t| match t {
TypedAstToken::TypedDeclaration(TyDecl::VariableDecl(var_decl)) => {
Expand Down
Loading

0 comments on commit 5f82bac

Please sign in to comment.