Skip to content

Commit

Permalink
LSP: Rewrite TyProgram traversal to use the Parse trait. (FuelLab…
Browse files Browse the repository at this point in the history
…s#4467)

## Description
This PR finishes off migrating to use the `Parse` trait during AST
traversal that was started in FuelLabs#4220. Once again, the diff is quite big
but it's just essentially moving the logic that was there into the trait
impl's.

closes FuelLabs#3799
  • Loading branch information
JoshuaBatty authored Apr 19, 2023
1 parent 35fe324 commit 59919b4
Show file tree
Hide file tree
Showing 4 changed files with 967 additions and 1,018 deletions.
14 changes: 9 additions & 5 deletions sway-lsp/src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ impl Session {
self.token_map.clear();
self.runnables.clear();

// Create context with write guards to make readers wait until the update to token_map is complete.
// This operation is fast because we already have the compile results.
let ctx = ParseContext::new(&self.token_map, Engines::new(&type_engine, &decl_engine));

let results_len = results.len();
for (i, res) in results.into_iter().enumerate() {
// We can convert these destructured elements to a Vec<Diagnostic> later on.
Expand All @@ -216,6 +212,14 @@ impl Session {
LanguageServerError::FailedToParse
})?;

// Create context with write guards to make readers wait until the update to token_map is complete.
// This operation is fast because we already have the compile results.
let ctx = ParseContext::new(
&self.token_map,
Engines::new(&type_engine, &decl_engine),
&typed_program.root.namespace,
);

// The final element in the results is the main program.
if i == results_len - 1 {
// First, populate our token_map with sway keywords.
Expand All @@ -229,7 +233,7 @@ impl Session {
// Finally, create runnables and populate our token_map with typed ast nodes.
self.create_runnables(typed_program, &decl_engine);

let typed_tree = TypedTree::new(&ctx, &typed_program.root.namespace);
let typed_tree = TypedTree::new(&ctx);
typed_tree.collect_module_spans(typed_program);
self.parse_ast_to_typed_tokens(typed_program, &ctx, |node, _ctx| {
typed_tree.traverse_node(node)
Expand Down
1 change: 1 addition & 0 deletions sway-lsp/src/core/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub enum TypedAstToken {
TypedDeclaration(ty::TyDecl),
TypedExpression(ty::TyExpression),
TypedScrutinee(ty::TyScrutinee),
TyStructScrutineeField(ty::TyStructScrutineeField),
TypedConstantDeclaration(ty::TyConstantDecl),
TypedFunctionDeclaration(ty::TyFunctionDecl),
TypedFunctionParameter(ty::TyFunctionParameter),
Expand Down
11 changes: 8 additions & 3 deletions sway-lsp/src/traverse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core::token_map::TokenMap;
use sway_core::Engines;
use sway_core::{namespace::Module, Engines};

pub(crate) mod dependency;
pub(crate) mod lexed_tree;
Expand All @@ -9,11 +9,16 @@ pub(crate) mod typed_tree;
pub struct ParseContext<'a> {
tokens: &'a TokenMap,
engines: Engines<'a>,
namespace: &'a Module,
}

impl<'a> ParseContext<'a> {
pub fn new(tokens: &'a TokenMap, engines: Engines<'a>) -> Self {
Self { tokens, engines }
pub fn new(tokens: &'a TokenMap, engines: Engines<'a>, namespace: &'a Module) -> Self {
Self {
tokens,
engines,
namespace,
}
}
}

Expand Down
Loading

0 comments on commit 59919b4

Please sign in to comment.