Skip to content

Commit

Permalink
test progress
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Oct 12, 2023
1 parent 30fb1fd commit 3674594
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 37 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sway-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ criterion = "0.5"
dirs = "4.0"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
sway-lsp-test-utils = { path = "tests/utils" }
pretty_assertions = "1.4.0"
tower = { version = "0.4.12", default-features = false, features = ["util"] }

[[bench]]
Expand Down
14 changes: 8 additions & 6 deletions sway-lsp/src/capabilities/code_actions/diagnostic/auto_import.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{cmp::Ordering, collections::HashMap};

use crate::{
capabilities::{code_actions::CodeActionContext, diagnostic::DiagnosticData},
core::token::{get_range_from_span, AstToken, SymbolKind, TypedAstToken},
Expand All @@ -15,7 +14,6 @@ use sway_core::language::{
CallPath,
};
use sway_types::{Ident, Spanned};

use super::CODE_ACTION_IMPORT_TITLE;

/// Returns a list of [CodeActionOrCommand] suggestions for inserting a missing import.
Expand Down Expand Up @@ -59,6 +57,7 @@ pub(crate) fn import_code_action(
// 4. Insert the import on a new line after the program type statement (e.g. `contract;`)
// 5. If all else fails, insert it at the beginning of the file.

// TODO: combine into 1 function and write unit test for location in the file
let text_edit: TextEdit = get_text_edit_for_group(&call_path, &use_statements)
.or_else(|| get_text_edit_in_use_block(&call_path, &use_statements))
.unwrap_or(get_text_edit_fallback(
Expand Down Expand Up @@ -89,13 +88,14 @@ pub(crate) fn import_code_action(
None
}

/// Returns an [Iterator] of [CallPath]s that match the given symbol name.
/// Returns an [Iterator] of [CallPath]s that match the given symbol name. The [CallPath]s are sorted
/// alphabetically.
fn get_call_paths_for_name<'s>(
ctx: &'s CodeActionContext,
symbol_name: &'s String,
) -> Option<impl 's + Iterator<Item = CallPath>> {
let namespace = ctx.namespace.to_owned()?;
Some(
let mut call_paths =
ctx.tokens
.tokens_for_name(symbol_name)
.filter_map(move |(_, token)| {
Expand Down Expand Up @@ -135,8 +135,10 @@ fn get_call_paths_for_name<'s>(
}
_ => None,
}
}),
)
}).collect::<Vec<_>>();
call_paths.sort();
Some(call_paths.into_iter())

}

/// If there is an existing [TyUseStatement] with the same prefix as the given [CallPath], returns a
Expand Down
13 changes: 13 additions & 0 deletions sway-lsp/tests/fixtures/auto_import/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "auto_import"
source = "member"
dependencies = ["std"]

[[package]]
name = "core"
source = "path+from-root-C29FCA7313EFC5E1"

[[package]]
name = "std"
source = "path+from-root-C29FCA7313EFC5E1"
dependencies = ["core"]
1 change: 1 addition & 0 deletions sway-lsp/tests/fixtures/auto_import/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ name = "auto_import"

[dependencies]
std = { path = "../../../../sway-lib-std" }
core = { path = "../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ library;

pub fn deep_fun(){}

pub const A: u32 = 0;

pub enum DeepEnum {
Variant: (),
Number: u32,
Expand All @@ -12,3 +10,9 @@ pub enum DeepEnum {
pub struct DeepStruct<T> {
field: T,
}

pub type A = DeepStruct<u32>;

pub trait DeepTrait {
fn deep_method(self);
}
25 changes: 22 additions & 3 deletions sway-lsp/tests/fixtures/auto_import/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract;
mod test_mod;
mod deep_mod;

use test_mod::A;
use test_mod::test_fun;

pub fn fun() {
let _ = EvmAddress {
Expand All @@ -14,7 +14,26 @@ pub fn fun() {
deep_fun();
A::fun();

let _ = ZERO_B256; // l 18 c 19
let _ = DeepEnum::Variant; // TODO: open an issue for this
let a: DeepEnum = DeepEnum::Variant; // TODO: open an issue for variants
let _ = DeepStruct::<u64> { field: 0 };

let _ = TEST_CONST;
let _ = ZERO_B256; // TODO: fix this

let _ = overflow(); // TODO: fix this
let _: Result<Identity, AuthError> = msg_sender();
}

struct LocalStruct {
field: u64,
}

impl DeepTrait for LocalStruct {
fn deep_method(self) {}
}

impl TryFrom<u32> for LocalStruct {
fn try_from(u: u32) -> Option<Self> {
None
}
}
2 changes: 2 additions & 0 deletions sway-lsp/tests/fixtures/auto_import/src/test_mod.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pub struct A {}
impl A {
pub fn fun() {}
}

pub const TEST_CONST: u64 = 111;
Loading

0 comments on commit 3674594

Please sign in to comment.