Skip to content

Commit

Permalink
fix(merging): refactored merging nodes
Browse files Browse the repository at this point in the history
Now it correctly handles multiple extends and nested extends.
  • Loading branch information
alesbrelih committed Dec 25, 2024
1 parent 2761c3b commit 9572886
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 106 deletions.
58 changes: 50 additions & 8 deletions src/gitlab_ci_ls_parser/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ use crate::gitlab_ci_ls_parser::{

use super::{
fs_utils, parser, parser_utils, treesitter, CompletionResult, Component, ComponentInput,
DefinitionResult, GitlabElement, GitlabInputElement, HoverResult, IncludeInformation,
LSPCompletion, LSPConfig, LSPLocation, LSPPosition, LSPResult, Range, ReferencesResult,
RemoteInclude, RuleReference,
DefinitionResult, GitlabElement, GitlabFileElements, GitlabInputElement, HoverResult,
IncludeInformation, LSPCompletion, LSPConfig, LSPLocation, LSPPosition, LSPResult, Range,
ReferencesResult, RemoteInclude, RuleReference,
};

#[allow(clippy::module_name_repetitions)]
pub struct LSPHandlers {
cfg: LSPConfig,
store: Mutex<HashMap<String, String>>,
nodes: Mutex<HashMap<String, HashMap<String, String>>>,
// ordered list by imports -> meaning it starts at root element and parses from top down as
// parser would do
// Also added a new wrapper so all jobs are separated by file in which they are located
// This was done so we can still keep the order but elements are inside file objects so
// when on_change occurs we can just wipe jobs inside that file structure.
// else we wouldn't know if elements were deleted or changed and there would be more code
nodes_ordered_list: Mutex<Vec<GitlabFileElements>>,
stages: Mutex<HashMap<String, GitlabElement>>,
variables: Mutex<HashMap<String, GitlabElement>>,
components: Mutex<HashMap<String, Component>>,
Expand All @@ -47,6 +54,7 @@ impl LSPHandlers {
cfg: cfg.clone(),
store,
nodes,
nodes_ordered_list: vec![].into(),
stages,
variables,
components,
Expand All @@ -64,6 +72,10 @@ impl LSPHandlers {
error!("error indexing workspace; err: {}", err);
}

//if let Err(err) = events.build_all_nodes(cfg.clone()) {
// error!("error building all nodes; err: {}", err)
//}

events
}

Expand All @@ -90,6 +102,7 @@ impl LSPHandlers {
let params = serde_json::from_value::<HoverParams>(request.params).ok()?;

let store = self.store.lock().unwrap();
let node_list = self.nodes_ordered_list.lock().unwrap();
let nodes = self.nodes.lock().unwrap();

let uri = &params.text_document_position_params.text_document.uri;
Expand All @@ -113,7 +126,7 @@ impl LSPHandlers {
uri: document_uri.to_string(),
..Default::default()
},
&store,
&node_list,
) {
Ok(c) => c,
Err(err) => return Some(LSPResult::Error(err)),
Expand Down Expand Up @@ -142,7 +155,7 @@ impl LSPHandlers {
uri: document_uri.clone(),
..Default::default()
},
&store,
&node_list,
) {
Ok(c) => c,
Err(err) => return Some(LSPResult::Error(err)),
Expand All @@ -168,7 +181,7 @@ impl LSPHandlers {
uri: document_uri.to_string(),
..Default::default()
},
&store,
&node_list,
) {
Ok(c) => c,
Err(err) => return Some(LSPResult::Error(err)),
Expand Down Expand Up @@ -201,7 +214,7 @@ impl LSPHandlers {
uri: document_uri.to_string(),
..Default::default()
},
&store,
&node_list,
) {
Ok(c) => c,
Err(err) => return Some(LSPResult::Error(err)),
Expand Down Expand Up @@ -234,6 +247,7 @@ impl LSPHandlers {

let mut store = self.store.lock().unwrap();
let mut all_nodes = self.nodes.lock().unwrap();
let mut all_nodes_ordered_list = self.nodes_ordered_list.lock().unwrap();
// reset previous
all_nodes.insert(params.text_document.uri.to_string(), HashMap::new());

Expand All @@ -250,14 +264,27 @@ impl LSPHandlers {
store.insert(file.path, file.content);
}

for node in results.nodes {
for node in results.nodes.clone() {
info!("found node: {:?}", &node);
all_nodes
.entry(node.uri)
.or_default()
.insert(node.key, node.content?);
}

if let Some(e) = all_nodes_ordered_list
.iter_mut()
.find(|e| e.uri == params.text_document.uri.to_string())
{
e.elements.clone_from(&results.nodes);
} else {
// new file?
all_nodes_ordered_list.push(GitlabFileElements {
uri: params.text_document.uri.to_string(),
elements: results.nodes.clone(),
});
}

if !results.stages.is_empty() {
let mut all_stages = self.stages.lock().unwrap();
all_stages.clear();
Expand Down Expand Up @@ -337,6 +364,7 @@ impl LSPHandlers {

let store = self.store.lock().unwrap();
let store = &*store;
let node_list = self.nodes_ordered_list.lock().unwrap();
let document_uri = params.text_document_position_params.text_document.uri;
let document = store.get::<String>(&document_uri.to_string())?;
let position = params.text_document_position_params.position;
Expand Down Expand Up @@ -434,6 +462,7 @@ impl LSPHandlers {
document_uri.as_str(),
position,
store,
&node_list,
)?;

for location in variable_locations {
Expand Down Expand Up @@ -940,6 +969,7 @@ impl LSPHandlers {
Ok(items)
}

#[allow(clippy::too_many_lines)]
fn index_workspace(&self, root_dir: &str) -> anyhow::Result<()> {
let mut in_progress = self.indexing_in_progress.lock().unwrap();
*in_progress = true;
Expand All @@ -948,6 +978,7 @@ impl LSPHandlers {

let mut store = self.store.lock().unwrap();
let mut all_nodes = self.nodes.lock().unwrap();
let mut all_nodes_ordered_list = self.nodes_ordered_list.lock().unwrap();
let mut all_stages = self.stages.lock().unwrap();
let mut all_variables = self.variables.lock().unwrap();
let mut all_components = self.components.lock().unwrap();
Expand Down Expand Up @@ -1025,6 +1056,17 @@ impl LSPHandlers {
store.insert(file.path, file.content);
}

for n in &results.nodes {
if let Some(el) = all_nodes_ordered_list.iter_mut().find(|e| e.uri == n.uri) {
el.elements.push(n.clone());
} else {
all_nodes_ordered_list.push(GitlabFileElements {
uri: n.uri.clone(),
elements: vec![n.clone()],
});
}
}

for node in results.nodes {
info!("found node: {:?}", &node);
let content = node.content.unwrap_or(String::new());
Expand Down
19 changes: 16 additions & 3 deletions src/gitlab_ci_ls_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub mod parser_utils;
pub mod treesitter;
pub mod treesitter_queries;

#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq, Hash)]
pub struct LSPPosition {
pub line: u32,
pub character: u32,
}

#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq, Hash)]
pub struct Range {
pub start: LSPPosition,
pub end: LSPPosition,
Expand Down Expand Up @@ -100,14 +100,27 @@ pub struct GitlabFile {
pub content: String,
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Hash)]
pub struct GitlabElement {
pub key: String,
pub content: Option<String>,
pub uri: String,
pub range: Range,
}

#[derive(Debug, Default, Clone, Hash)]
pub struct GitlabFileElements {
pub uri: String,
pub elements: Vec<GitlabElement>,
}

#[derive(Debug, Default, Clone, Hash)]
pub struct GitlabElementWithParentAndLvl {
pub el: GitlabElement,
pub parents: String,
pub lvl: usize,
}

#[derive(Debug, Default, Clone)]
pub struct GitlabCacheElement {
pub key: String,
Expand Down
Loading

0 comments on commit 9572886

Please sign in to comment.