Skip to content

Commit

Permalink
Add goto and hover tests for TyConstantDeclaration tokens (FuelLabs…
Browse files Browse the repository at this point in the history
…#3990)

## Description
This PR adds tests for go_to and hover functionality for
`TyConstantDeclaration` tokens.

Hover requests have been reworked so be inline with how go_to requests
are structured.

works towards: FuelLabs#3989
  • Loading branch information
JoshuaBatty authored Feb 7, 2023
1 parent 5c02668 commit ddd1b8b
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 34 deletions.
161 changes: 134 additions & 27 deletions sway-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ mod tests {
def_path: &'a str,
}

/// Contains data required to evaluate a hover request response.
struct HoverDocumentation<'a> {
req_uri: &'a Url,
req_line: i32,
req_char: i32,
documentation: &'a str,
}

fn load_sway_example(src_path: PathBuf) -> (Url, String) {
let mut file = fs::File::open(&src_path).unwrap();
let mut sway_program = String::new();
Expand Down Expand Up @@ -861,38 +869,30 @@ mod tests {
definition
}

async fn hover_request(service: &mut LspService<Backend>, uri: &Url) -> Request {
async fn hover_request<'a>(
service: &mut LspService<Backend>,
hover_docs: &'a HoverDocumentation<'a>,
id: i64,
) -> Request {
let params = json!({
"textDocument": {
"uri": uri,
"uri": hover_docs.req_uri,
},
"position": {
"line": 44,
"character": 24
"line": hover_docs.req_line,
"character": hover_docs.req_char
}
});
let hover = build_request_with_id("textDocument/hover", params, 1);
let response = call_request(service, hover.clone()).await;
let expected = Response::from_ok(
1.into(),
json!({
"contents": {
"kind": "markdown",
"value": "```sway\nstruct Data\n```\n---\n Struct holding:\n\n 1. A `value` of type `NumberOrString`\n 2. An `address` of type `u64`"
},
"range": {
"end": {
"character": 27,
"line": 44
},
"start": {
"character": 23,
"line": 44
}
}
}),
);
assert_json_eq!(expected, response.ok().unwrap());
let hover = build_request_with_id("textDocument/hover", params, id);
let response = call_request(service, hover.clone()).await.unwrap().unwrap();
let value = response.result().unwrap().clone();
let hover_res: Hover = serde_json::from_value(value).unwrap();

if let HoverContents::Markup(markup_content) = hover_res.contents {
assert_eq!(hover_docs.documentation, markup_content.value);
} else {
panic!("Expected HoverContents::Markup");
}
hover
}

Expand Down Expand Up @@ -1755,9 +1755,117 @@ mod tests {
go_to.def_end_char = 17;
definition_check_with_req_offset(&mut service, &mut go_to, 53, 29, 9).await;

// Variable type ascriptions
go_to.def_line = 6;
go_to.def_start_char = 5;
go_to.def_end_char = 16;
definition_check_with_req_offset(&mut service, &mut go_to, 56, 21, 10).await;

shutdown_and_exit(&mut service).await;
}

#[tokio::test]
async fn go_to_definition_for_consts() {
let (mut service, _) = LspService::new(Backend::new);
let uri = init_and_open(
&mut service,
test_fixtures_dir().join("tokens/consts/src/main.sw"),
)
.await;

// value: TyExpression
let mut contract_go_to = GotoDefintion {
req_uri: &uri,
req_line: 9,
req_char: 24,
def_line: 18,
def_start_char: 5,
def_end_char: 9,
def_path: "sway-lib-std/src/contract_id.sw",
};
let _ = definition_check(&mut service, &contract_go_to, 1).await;

contract_go_to.req_char = 34;
contract_go_to.def_line = 19;
contract_go_to.def_start_char = 7;
contract_go_to.def_end_char = 11;
let _ = definition_check(&mut service, &contract_go_to, 2).await;

// Constants defined in the same module
let mut go_to = GotoDefintion {
req_uri: &uri,
req_line: 19,
req_char: 34,
def_line: 6,
def_start_char: 6,
def_end_char: 16,
def_path: uri.as_str(),
};
let _ = definition_check(&mut service, &contract_go_to, 3).await;

go_to.def_line = 9;
definition_check_with_req_offset(&mut service, &mut go_to, 20, 29, 4).await;

// Constants defined in a different module
go_to = GotoDefintion {
req_uri: &uri,
req_line: 23,
req_char: 73,
def_line: 12,
def_start_char: 10,
def_end_char: 20,
def_path: "consts/src/more_consts.sw",
};
let _ = definition_check(&mut service, &go_to, 5).await;

go_to.def_line = 13;
go_to.def_start_char = 10;
go_to.def_end_char = 18;
definition_check_with_req_offset(&mut service, &mut go_to, 24, 31, 6).await;

// Constants with type ascriptions
go_to.def_line = 6;
go_to.def_start_char = 5;
go_to.def_end_char = 9;
definition_check_with_req_offset(&mut service, &mut go_to, 10, 17, 7).await;
}

#[tokio::test]
async fn hover_docs_for_consts() {
let (mut service, _) = LspService::new(Backend::new);
let uri = init_and_open(
&mut service,
test_fixtures_dir().join("tokens/consts/src/main.sw"),
)
.await;

let mut hover = HoverDocumentation {
req_uri: &uri,
req_line: 19,
req_char: 33,
documentation: " documentation for CONSTANT_1",
};

let _ = hover_request(&mut service, &hover, 1).await;
hover.req_char = 49;
hover.documentation = " CONSTANT_2 has a value of 200";
let _ = hover_request(&mut service, &hover, 2).await;
}

#[tokio::test]
async fn hover_docs_with_code_examples() {
let (mut service, _) = LspService::new(Backend::new);
let uri = init_and_open(&mut service, doc_comments_dir().join("src/main.sw")).await;

let hover = HoverDocumentation {
req_uri: &uri,
req_line: 44,
req_char: 24,
documentation: "```sway\nstruct Data\n```\n---\n Struct holding:\n\n 1. A `value` of type `NumberOrString`\n 2. An `address` of type `u64`",
};
let _ = hover_request(&mut service, &hover, 1).await;
}

#[tokio::test]
async fn publish_diagnostics_dead_code_warning() {
let (mut service, socket) = LspService::new(Backend::new);
Expand Down Expand Up @@ -1814,7 +1922,6 @@ mod tests {
format_request,
doc_comments_dir().join("src/main.sw")
);
lsp_capability_test!(hover, hover_request, doc_comments_dir().join("src/main.sw"));
lsp_capability_test!(
highlight,
highlight_request,
Expand Down
11 changes: 5 additions & 6 deletions sway-lsp/src/utils/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl KeywordDocs {
};

let const_keyword: ItemMod = parse_quote! {
/// ## Compile-time constants.
/// Compile-time constants.
///
/// Sometimes a certain value is used many times throughout a program, and it can become
/// inconvenient to copy it over and over. What's more, it's not always possible or desirable to
Expand Down Expand Up @@ -841,10 +841,9 @@ async fn keywords_in_sync() {
.collect();

for keyword in &compiler_keywords {
if !lsp_keywords.contains(&keyword) {
let err =
format!("Error: Documention for the `{keyword}` keyword is not implemented in LSP");
panic!("{err}");
}
assert!(
lsp_keywords.contains(&keyword),
"Error: Documentation for the `{keyword}` keyword is not implemented in LSP"
);
}
}
2 changes: 2 additions & 0 deletions sway-lsp/test/fixtures/tokens/consts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
8 changes: 8 additions & 0 deletions sway-lsp/test/fixtures/tokens/consts/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "consts"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
26 changes: 26 additions & 0 deletions sway-lsp/test/fixtures/tokens/consts/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
contract;

dep more_consts;
use more_consts::{Data, Value};

/// documentation for CONSTANT_1
const CONSTANT_1 = 100;
/// CONSTANT_2 has a value of 200
const CONSTANT_2: u32 = 200;
const BASE_TOKEN = ContractId::from(0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c);
const MY_DATA: Data = Data::B(Value {a: 100});

struct Point {
x: u64,
y: u32,
}

fn test() {
// Constants defined in the same module
let point = Point { x: CONSTANT_1, y: CONSTANT_2 };
let contract_id = BASE_TOKEN;

// Constants defined in a different module
let point = Point { x: more_consts::CONSTANT_3, y: more_consts::CONSTANT_4 };
let data = more_consts::MY_DATA1;
}
14 changes: 14 additions & 0 deletions sway-lsp/test/fixtures/tokens/consts/src/more_consts.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
library more_consts;

struct Value {
a: u32,
}

enum Data {
A: bool,
B: Value,
}

pub const CONSTANT_3: u32 = 300;
pub const CONSTANT_4: u32 = 400;
pub const MY_DATA1: Data = Data::A(true);
5 changes: 4 additions & 1 deletion sway-lsp/test/fixtures/tokens/variables/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ fn main() {

// Variable usage: Shadowing
let variable5 = variable3;
}

// Variable type ascriptions
let variable6: ExampleEnum = ExampleEnum::Variants(101);
}

0 comments on commit ddd1b8b

Please sign in to comment.