Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Lsp compile unit cache watcher for config file(kcl.yaml and kcl.mod). #1188

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::goto_def::{find_def_with_gs, goto_definition_with_gs};
use crate::to_lsp::lsp_location;
use crate::util::{compile_with_params, Params};

use crate::state::{KCLVfs, KCLWordIndexMap};
use crate::state::{KCLCompileUnitCache, KCLVfs, KCLWordIndexMap};
use anyhow::Result;
use kclvm_ast::ast::Program;
use kclvm_error::Position as KCLPos;
Expand All @@ -24,6 +24,7 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
gs: &GlobalState,
module_cache: Option<KCLModuleCache>,
scope_cache: Option<KCLScopeCache>,
compile_unit_cache: Option<KCLCompileUnitCache>,
) -> Result<Vec<Location>, String> {
let def = find_def_with_gs(kcl_pos, gs, true);
match def {
Expand All @@ -42,6 +43,7 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
logger,
module_cache,
scope_cache,
compile_unit_cache,
))
} else {
Err(format!("Invalid file path: {0}", start.filename))
Expand All @@ -67,6 +69,7 @@ pub(crate) fn find_refs_from_def<F: Fn(String) -> Result<(), anyhow::Error>>(
logger: F,
module_cache: Option<KCLModuleCache>,
scope_cache: Option<KCLScopeCache>,
compile_unit_cache: Option<KCLCompileUnitCache>,
) -> Vec<Location> {
let mut ref_locations = vec![];
for word_index in (*word_index_map.write()).values_mut() {
Expand All @@ -92,6 +95,7 @@ pub(crate) fn find_refs_from_def<F: Fn(String) -> Result<(), anyhow::Error>>(
module_cache: module_cache.clone(),
scope_cache: scope_cache.clone(),
vfs: vfs.clone(),
compile_unit_cache: compile_unit_cache.clone(),
}) {
Ok((prog, _, gs)) => {
let ref_pos = kcl_pos(&file_path, ref_loc.range.start);
Expand Down Expand Up @@ -216,6 +220,7 @@ mod tests {
logger,
None,
None,
None,
),
);
}
Expand Down Expand Up @@ -273,6 +278,7 @@ mod tests {
logger,
None,
None,
None,
),
);
}
Expand Down Expand Up @@ -330,6 +336,7 @@ mod tests {
logger,
None,
None,
None,
),
);
}
Expand Down Expand Up @@ -380,6 +387,7 @@ mod tests {
logger,
None,
None,
None,
),
);
}
Expand Down
9 changes: 8 additions & 1 deletion kclvm/tools/src/LSP/src/notification.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use kclvm_config::{modfile::KCL_MOD_FILE, settings::DEFAULT_SETTING_FILE};
use lsp_types::notification::{
Cancel, DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument,
DidOpenTextDocument, DidSaveTextDocument,
Expand Down Expand Up @@ -141,8 +142,14 @@ impl LanguageServerState {
) -> anyhow::Result<()> {
for change in params.changes {
let path = from_lsp::abs_path(&change.uri)?;
self.loader.handle.invalidate(path);
self.loader.handle.invalidate(path.clone());
if KCL_CONFIG_FILE.contains(&path.file_name().unwrap().to_str().unwrap()) {
self.compile_unit_cache.write().clear();
}
}

Ok(())
}
}

const KCL_CONFIG_FILE: [&'static str; 2] = [DEFAULT_SETTING_FILE, KCL_MOD_FILE];
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/quick_fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ mod tests {
module_cache: None,
scope_cache: None,
vfs: Some(KCLVfs::default()),
compile_unit_cache: None,
})
.unwrap();

Expand Down
47 changes: 9 additions & 38 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::{
hover, quick_fix,
semantic_token::semantic_tokens_full,
state::{log_message, LanguageServerSnapshot, LanguageServerState, Task},
util::{compile_with_params, Params},
};

impl LanguageServerState {
Expand Down Expand Up @@ -267,7 +266,8 @@ pub(crate) fn handle_reference(
let pos = kcl_pos(&file, params.text_document_position.position);
let log = |msg: String| log_message(msg, &sender);
let module_cache = snapshot.module_cache.clone();
let scope_cache = snapshot.scope_cache.clone();
let _scope_cache = snapshot.scope_cache.clone();
let compile_unit_cache = snapshot.compile_unit_cache.clone();
match find_refs(
&db.prog,
&pos,
Expand All @@ -276,8 +276,9 @@ pub(crate) fn handle_reference(
Some(snapshot.vfs.clone()),
log,
&db.gs,
module_cache,
scope_cache,
Some(module_cache),
None,
Some(compile_unit_cache),
) {
core::result::Result::Ok(locations) => Ok(Some(locations)),
Err(msg) => {
Expand Down Expand Up @@ -305,38 +306,7 @@ pub(crate) fn handle_completion(
.and_then(|ctx| ctx.trigger_character)
.and_then(|s| s.chars().next());

let db =
match completion_trigger_character {
// Some trigger characters need to re-compile
Some(ch) => match ch {
'=' | ':' => {
let file_id = snapshot.vfs.read().file_id(&path.clone().into()).ok_or(
anyhow::anyhow!(LSPError::FileIdNotFound(path.clone().into())),
)?;
let version = *snapshot.opened_files.read().get(&file_id).ok_or_else(|| {
anyhow::anyhow!(LSPError::DocumentVersionNotFound(path.clone().into()))
})?;

match compile_with_params(Params {
file: file.clone(),
module_cache: snapshot.module_cache,
scope_cache: None,
vfs: Some(snapshot.vfs.clone()),
}) {
Ok((prog, diags, gs)) => Arc::new(AnalysisDatabase {
prog,
diags,
gs,
version,
}),
Err(_) => return Ok(None),
}
}
_ => snapshot.get_db(&path.clone().into())?,
},

None => snapshot.get_db(&path.clone().into())?,
};
let db = snapshot.get_db(&path.clone().into())?;
Peefy marked this conversation as resolved.
Show resolved Hide resolved

let res = completion(completion_trigger_character, &db.prog, &kcl_pos, &db.gs);

Expand Down Expand Up @@ -416,8 +386,9 @@ pub(crate) fn handle_rename(
Some(snapshot.vfs.clone()),
log,
&db.gs,
snapshot.module_cache.clone(),
snapshot.scope_cache.clone(),
Some(snapshot.module_cache),
Some(snapshot.scope_cache),
Some(snapshot.compile_unit_cache),
);
match references {
Result::Ok(locations) => {
Expand Down
36 changes: 23 additions & 13 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::util::{compile_with_params, get_file_name, to_json, Params};
use crate::word_index::build_word_index;
use anyhow::Result;
use crossbeam_channel::{select, unbounded, Receiver, Sender};
use kclvm_parser::KCLModuleCache;
use kclvm_sema::resolver::scope::{CachedScope, KCLScopeCache};
use kclvm_parser::{KCLModuleCache, LoadProgramOptions};
use kclvm_sema::resolver::scope::KCLScopeCache;
use lsp_server::{ReqQueue, Request, Response};
use lsp_types::Url;
use lsp_types::{
Expand All @@ -18,7 +18,6 @@ use lsp_types::{
use parking_lot::RwLock;
use ra_ap_vfs::{ChangeKind, ChangedFile, FileId, Vfs};
use std::collections::HashMap;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use std::{sync::Arc, time::Instant};
Expand Down Expand Up @@ -48,6 +47,8 @@ pub(crate) struct Handle<H, C> {

pub(crate) type KCLVfs = Arc<RwLock<Vfs>>;
pub(crate) type KCLWordIndexMap = Arc<RwLock<HashMap<Url, HashMap<String, Vec<Location>>>>>;
pub(crate) type KCLCompileUnitCache =
Arc<RwLock<HashMap<String, (Vec<String>, Option<LoadProgramOptions>)>>>;

/// State for the language server
pub(crate) struct LanguageServerState {
Expand Down Expand Up @@ -88,10 +89,13 @@ pub(crate) struct LanguageServerState {
pub word_index_map: KCLWordIndexMap,

/// KCL parse cache
pub module_cache: Option<KCLModuleCache>,
pub module_cache: KCLModuleCache,

/// KCL resolver cache
pub scope_cache: Option<KCLScopeCache>,
pub scope_cache: KCLScopeCache,

/// KCL compile unit cache cache
pub compile_unit_cache: KCLCompileUnitCache,
}

/// A snapshot of the state of the language server
Expand All @@ -106,9 +110,11 @@ pub(crate) struct LanguageServerSnapshot {
/// The word index map
pub word_index_map: KCLWordIndexMap,
/// KCL parse cache
pub module_cache: Option<KCLModuleCache>,
pub module_cache: KCLModuleCache,
/// KCL resolver cache
pub scope_cache: Option<KCLScopeCache>,
pub scope_cache: KCLScopeCache,
/// KCL compile unit cache cache
pub compile_unit_cache: KCLCompileUnitCache,
}

#[allow(unused)]
Expand Down Expand Up @@ -141,8 +147,9 @@ impl LanguageServerState {
opened_files: Arc::new(RwLock::new(HashMap::new())),
word_index_map: Arc::new(RwLock::new(HashMap::new())),
loader,
module_cache: Some(KCLModuleCache::default()),
scope_cache: Some(Arc::new(Mutex::new(CachedScope::default()))),
module_cache: KCLModuleCache::default(),
scope_cache: KCLScopeCache::default(),
compile_unit_cache: KCLCompileUnitCache::default(),
};

let word_index_map = state.word_index_map.clone();
Expand Down Expand Up @@ -228,18 +235,20 @@ impl LanguageServerState {
self.thread_pool.execute({
let mut snapshot = self.snapshot();
let sender = self.task_sender.clone();
let module_cache = self.module_cache.clone();
let scope_cache = self.scope_cache.clone();
let module_cache = Arc::clone(&self.module_cache);
let scope_cache = Arc::clone(&self.scope_cache);
let compile_unit_cache = Arc::clone(&self.compile_unit_cache);
move || match url(&snapshot, file.file_id) {
Ok(uri) => {
let version =
snapshot.opened_files.read().get(&file.file_id).cloned();
let mut db = snapshot.db.write();
match compile_with_params(Params {
file: filename.clone(),
module_cache,
scope_cache: None,
module_cache: Some(module_cache),
scope_cache: Some(scope_cache),
vfs: Some(snapshot.vfs),
compile_unit_cache: Some(compile_unit_cache),
}) {
Ok((prog, diags, gs)) => {
let current_version = snapshot
Expand Down Expand Up @@ -374,6 +383,7 @@ impl LanguageServerState {
word_index_map: self.word_index_map.clone(),
module_cache: self.module_cache.clone(),
scope_cache: self.scope_cache.clone(),
compile_unit_cache: self.compile_unit_cache.clone(),
}
}

Expand Down
Loading
Loading