Skip to content

Commit ce7692d

Browse files
sway server improvements (#302)
* make types public for sway-server * make pub * add utils * adjust to new token type * improve token with token type * make pub * update hover for enum and trait * added visibility for trait * adjust to var details * extract var function * added new token functions * updated hover logic
1 parent edd709e commit ce7692d

File tree

18 files changed

+301
-169
lines changed

18 files changed

+301
-169
lines changed

core_lang/src/parse_tree/declaration/function_declaration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use pest::iterators::Pair;
99
use std::collections::HashMap;
1010

1111
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12-
pub(crate) enum Visibility {
12+
pub enum Visibility {
1313
Public,
1414
Private,
1515
}
@@ -26,10 +26,10 @@ impl Visibility {
2626
#[derive(Debug, Clone)]
2727
pub struct FunctionDeclaration<'sc> {
2828
pub name: Ident<'sc>,
29-
pub(crate) visibility: Visibility,
29+
pub visibility: Visibility,
3030
pub body: CodeBlock<'sc>,
3131
pub(crate) parameters: Vec<FunctionParameter<'sc>>,
32-
pub(crate) span: Span<'sc>,
32+
pub span: Span<'sc>,
3333
pub(crate) return_type: TypeInfo<'sc>,
3434
pub(crate) type_parameters: Vec<TypeParameter<'sc>>,
3535
pub(crate) return_type_span: Span<'sc>,

core_lang/src/parse_tree/declaration/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ mod variable_declaration;
1212
pub(crate) use abi_declaration::*;
1313
pub(crate) use constant_declaration::*;
1414
pub(crate) use enum_declaration::*;
15-
pub(crate) use function_declaration::*;
15+
pub use function_declaration::*;
1616
pub(crate) use impl_trait::*;
1717
pub(crate) use reassignment::*;
18-
pub(crate) use struct_declaration::*;
19-
pub(crate) use trait_declaration::*;
18+
pub use struct_declaration::*;
19+
pub use trait_declaration::*;
2020
pub(crate) use type_parameter::*;
2121
pub use variable_declaration::*;
2222

core_lang/src/parse_tree/declaration/struct_declaration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct StructDeclaration<'sc> {
1616
pub name: Ident<'sc>,
1717
pub(crate) fields: Vec<StructField<'sc>>,
1818
pub(crate) type_parameters: Vec<TypeParameter<'sc>>,
19-
pub(crate) visibility: Visibility,
19+
pub visibility: Visibility,
2020
}
2121

2222
#[derive(Debug, Clone)]

core_lang/src/parse_tree/declaration/trait_declaration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct TraitDeclaration<'sc> {
1616
pub(crate) interface_surface: Vec<TraitFn<'sc>>,
1717
pub(crate) methods: Vec<FunctionDeclaration<'sc>>,
1818
pub(crate) type_parameters: Vec<TypeParameter<'sc>>,
19-
pub(crate) visibility: Visibility,
19+
pub visibility: Visibility,
2020
}
2121

2222
impl<'sc> TraitDeclaration<'sc> {

core_lang/src/span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::PathBuf;
22

33
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
44
pub struct Span<'sc> {
5-
pub(crate) span: pest::Span<'sc>,
5+
pub span: pest::Span<'sc>,
66
pub(crate) path: Option<PathBuf>,
77
}
88

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::core::{
2-
session::Session,
3-
token::{ContentType, DeclarationType, Token},
4-
};
1+
use crate::core::{session::Session, token::Token, token_type::TokenType};
52
use lspower::lsp::{CompletionItem, CompletionItemKind, CompletionParams, CompletionResponse};
63
use std::sync::Arc;
74

@@ -24,7 +21,7 @@ pub fn to_completion_items(tokens: &Vec<Token>) -> Vec<CompletionItem> {
2421
if token.is_initial_declaration() {
2522
let item = CompletionItem {
2623
label: token.name.clone(),
27-
kind: get_kind(&token.content_type),
24+
kind: get_kind(&token.token_type),
2825
..Default::default()
2926
};
3027
completion_items.push(item);
@@ -34,18 +31,16 @@ pub fn to_completion_items(tokens: &Vec<Token>) -> Vec<CompletionItem> {
3431
completion_items
3532
}
3633

37-
fn get_kind(content_type: &ContentType) -> Option<CompletionItemKind> {
38-
if let ContentType::Declaration(dec) = content_type {
39-
match dec {
40-
DeclarationType::Enum => Some(CompletionItemKind::Enum),
41-
DeclarationType::Function => Some(CompletionItemKind::Function),
42-
DeclarationType::Library => Some(CompletionItemKind::Module),
43-
DeclarationType::Struct => Some(CompletionItemKind::Struct),
44-
DeclarationType::Variable => Some(CompletionItemKind::Variable),
45-
DeclarationType::Trait => Some(CompletionItemKind::Interface),
46-
_ => None,
34+
fn get_kind(token_type: &TokenType) -> Option<CompletionItemKind> {
35+
match token_type {
36+
TokenType::Enum => Some(CompletionItemKind::Enum),
37+
TokenType::FunctionDeclaration(_) | &TokenType::FunctionApplication => {
38+
Some(CompletionItemKind::Function)
4739
}
48-
} else {
49-
None
40+
TokenType::Library => Some(CompletionItemKind::Module),
41+
TokenType::Struct(_) => Some(CompletionItemKind::Struct),
42+
TokenType::Variable(_) => Some(CompletionItemKind::Variable),
43+
TokenType::Trait(_) => Some(CompletionItemKind::Interface),
44+
_ => None,
5045
}
5146
}
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
use std::sync::Arc;
2-
1+
use crate::core::{session::Session, token::Token, token_type::TokenType};
32
use lspower::lsp::{DocumentSymbolResponse, Location, SymbolInformation, SymbolKind, Url};
4-
5-
use crate::core::{
6-
session::Session,
7-
token::{ContentType, DeclarationType, Token},
8-
};
3+
use std::sync::Arc;
94

105
pub fn document_symbol(session: Arc<Session>, url: Url) -> Option<DocumentSymbolResponse> {
116
match session.get_symbol_information(&url) {
@@ -30,26 +25,22 @@ pub fn to_symbol_information(tokens: &Vec<Token>, url: Url) -> Vec<SymbolInforma
3025
fn create_symbol_info(token: &Token, url: Url) -> SymbolInformation {
3126
SymbolInformation {
3227
name: token.name.clone(),
33-
kind: get_kind(&token.content_type),
28+
kind: get_kind(&token.token_type),
3429
location: Location::new(url, token.range),
3530
tags: None,
3631
container_name: None,
3732
deprecated: None,
3833
}
3934
}
4035

41-
fn get_kind(content_type: &ContentType) -> SymbolKind {
42-
if let ContentType::Declaration(dec) = content_type {
43-
match dec {
44-
DeclarationType::Enum => SymbolKind::Enum,
45-
DeclarationType::Function => SymbolKind::Function,
46-
DeclarationType::Library => SymbolKind::Module,
47-
DeclarationType::Struct => SymbolKind::Struct,
48-
DeclarationType::Variable => SymbolKind::Variable,
49-
DeclarationType::Trait => SymbolKind::Interface,
50-
_ => SymbolKind::Unknown,
51-
}
52-
} else {
53-
SymbolKind::Unknown
36+
fn get_kind(token_type: &TokenType) -> SymbolKind {
37+
match token_type {
38+
TokenType::Enum => SymbolKind::Enum,
39+
TokenType::FunctionDeclaration(_) | &TokenType::FunctionApplication => SymbolKind::Function,
40+
TokenType::Library => SymbolKind::Module,
41+
TokenType::Struct(_) => SymbolKind::Struct,
42+
TokenType::Variable(_) => SymbolKind::Variable,
43+
TokenType::Trait(_) => SymbolKind::Interface,
44+
_ => SymbolKind::Unknown,
5445
}
5546
}
Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,97 @@
1-
use std::sync::Arc;
2-
3-
use crate::core::{session::Session, token::Token};
1+
use crate::{
2+
core::{
3+
session::{Documents, Session},
4+
token::Token,
5+
token_type::{TokenType, VarBody},
6+
},
7+
utils::common::extract_visibility,
8+
};
49
use lspower::lsp::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind};
10+
use std::sync::Arc;
511

612
pub fn get_hover_data(session: Arc<Session>, params: HoverParams) -> Option<Hover> {
713
let position = params.text_document_position_params.position;
814
let url = &params.text_document_position_params.text_document.uri;
915

10-
session.get_token_hover_content(url, position)
16+
match session.documents.get(url.path()) {
17+
Some(ref document) => {
18+
if let Some(token) = document.get_token_at_position(position) {
19+
if token.is_initial_declaration() {
20+
return Some(get_hover_format(token, &session.documents));
21+
} else {
22+
// todo: this logic is flawed at the moment
23+
// if there are multiple tokens with the same name and type in different files
24+
// there is no way for us to know which one is currently used in here
25+
for document_ref in &session.documents {
26+
if let Some(declared_token) = document_ref.get_declared_token(&token.name) {
27+
if declared_token.is_same_type(token) {
28+
return Some(get_hover_format(declared_token, &session.documents));
29+
}
30+
}
31+
}
32+
None
33+
}
34+
} else {
35+
None
36+
}
37+
}
38+
_ => None,
39+
}
1140
}
1241

13-
pub fn to_hover_content(token: &Token) -> Hover {
42+
fn get_hover_format(token: &Token, documents: &Documents) -> Hover {
43+
let value = match &token.token_type {
44+
TokenType::Variable(var_details) => {
45+
let var_type = match &var_details.var_body {
46+
VarBody::FunctionCall(fn_name) => get_var_type_from_fn(&fn_name, documents),
47+
VarBody::Type(var_type) => var_type.clone(),
48+
_ => "".into(),
49+
};
50+
51+
format!(
52+
"let{} {}: {}",
53+
if var_details.is_mutable { " mut" } else { "" },
54+
token.name,
55+
var_type
56+
)
57+
}
58+
TokenType::FunctionDeclaration(func_details) => func_details.signature.clone(),
59+
TokenType::Struct(struct_details) => format!(
60+
"{}struct {}",
61+
extract_visibility(&struct_details.visibility),
62+
&token.name
63+
),
64+
TokenType::Trait(trait_details) => format!(
65+
"{}trait {}",
66+
extract_visibility(&trait_details.visibility),
67+
&token.name
68+
),
69+
TokenType::Enum => format!("enum {}", &token.name),
70+
_ => token.name.clone(),
71+
};
72+
1473
Hover {
1574
contents: HoverContents::Markup(MarkupContent {
16-
value: format!("{:?} : {}", token.content_type, token.name),
17-
kind: MarkupKind::PlainText,
75+
value: format!("```sway\n{}\n```", value),
76+
kind: MarkupKind::Markdown,
1877
}),
1978
range: Some(token.range),
2079
}
2180
}
81+
82+
fn get_var_type_from_fn(fn_name: &str, documents: &Documents) -> String {
83+
for document_ref in documents {
84+
if let Some(declared_token) = document_ref.get_declared_token(&fn_name) {
85+
match &declared_token.token_type {
86+
TokenType::FunctionDeclaration(func_details) => {
87+
return func_details
88+
.get_return_type_from_signature()
89+
.unwrap_or_default();
90+
}
91+
_ => {}
92+
}
93+
}
94+
}
95+
96+
"".into()
97+
}

sway-server/src/capabilities/semantic_tokens.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::core::{
2-
session::Session,
3-
token::{ContentType, DeclarationType, Token},
4-
};
1+
use crate::core::{session::Session, token::Token, token_type::TokenType};
52
use lspower::lsp::{
63
SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens,
74
SemanticTokensFullOptions, SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams,
@@ -49,7 +46,7 @@ pub fn to_semantic_tokes(tokens: &Vec<Token>) -> Vec<SemanticToken> {
4946
fn create_semantic_token(next_token: &Token, prev_token: Option<&Token>) -> SemanticToken {
5047
// TODO - improve with modifiers
5148
let token_modifiers_bitset = 0;
52-
let token_type = get_type(&next_token.content_type);
49+
let token_type = get_type(&next_token.token_type);
5350
let length = next_token.length;
5451

5552
let next_token_start_char = next_token.range.start.character;
@@ -82,20 +79,16 @@ static ENUM: u32 = 10;
8279
static STRUCT: u32 = 11;
8380
static TRAIT: u32 = 12;
8481

85-
fn get_type(content_type: &ContentType) -> u32 {
86-
if let ContentType::Declaration(dec) = content_type {
87-
match dec {
88-
DeclarationType::Function => FUNCTION,
89-
DeclarationType::Library => LIBRARY,
90-
DeclarationType::Variable => VARIABLE,
91-
DeclarationType::Enum => ENUM,
92-
DeclarationType::Struct => STRUCT,
93-
DeclarationType::Trait => TRAIT,
94-
_ => VARIABLE,
95-
}
96-
} else {
82+
fn get_type(token_type: &TokenType) -> u32 {
83+
match token_type {
84+
TokenType::FunctionDeclaration(_) | &TokenType::FunctionApplication => FUNCTION,
85+
TokenType::Library => LIBRARY,
86+
TokenType::Variable(_) => VARIABLE,
87+
TokenType::Enum => ENUM,
88+
TokenType::Struct(_) => STRUCT,
89+
TokenType::Trait(_) => TRAIT,
9790
// currently we return `variable` type as default
98-
VARIABLE
91+
_ => VARIABLE,
9992
}
10093
}
10194

sway-server/src/core/document.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
use std::collections::HashMap;
2-
1+
use super::token::Token;
2+
use super::token_type::TokenType;
3+
use crate::{capabilities, core::token::traverse_node};
34
use core_lang::parse;
45
use lspower::lsp::{Diagnostic, Position, Range, TextDocumentContentChangeEvent};
5-
66
use ropey::Rope;
7-
8-
use crate::{
9-
capabilities,
10-
core::token::{traverse_node, DeclarationType},
11-
};
12-
13-
use super::token::{ContentType, Token};
7+
use std::collections::HashMap;
148

159
#[derive(Debug)]
1610
pub struct TextDocument {
@@ -124,10 +118,7 @@ impl TextDocument {
124118
for (ident, parse_tree) in value.library_exports {
125119
// TODO
126120
// Is library name necessary to store for the LSP?
127-
let token = Token::from_ident(
128-
ident,
129-
ContentType::Declaration(DeclarationType::Library),
130-
);
121+
let token = Token::from_ident(&ident, TokenType::Library);
131122
tokens.push(token);
132123
for node in parse_tree.root_nodes {
133124
traverse_node(node, &mut tokens);

0 commit comments

Comments
 (0)