Repository: kcl-lang/kcl (language: rust)

Issue:
[WIP] Feat : Support Call Kclvm Cli by C API
<!-- Thank you for contributing to KusionStack!

Note: 

1. With pull requests:

    - Open your pull request against "main"
    - Your pull request should have no more than two commits, if not you should squash them.
    - It should pass all tests in the available continuous integration systems such as GitHub Actions.
    - You should add/modify tests to cover your proposed code changes.
    - If your pull request contains a new feature, please document it on the README.

2. Please create an issue first to describe the problem.

    We recommend that link the issue with the PR in the following question.
    For more info, check https://kusionstack.io/docs/governance/contribute/
-->

#### 1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

- [ ] N
- [x] Y 
re #125 

#### 2. What is the scope of this PR (e.g. component or file name):

<!-- You can add the scope of this change here. 
-->
        KCLVM/kclvm/spec
        KCLVM/kclvm/src/lib.rs

#### 3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

<!-- You can choose a brief description here -->
- [ ] Affects user behaviors
- [ ] Contains syntax changes
- [ ] Contains variable changes
- [x] Contains experimental features
- [ ] Performance regression: Consumes more CPU
- [ ] Performance regression: Consumes more Memory
- [ ] Other

<!-- You can add more details here.
-->
    Support user to call kclvm_cli by c_api


#### 4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

- [x] N
- [ ] Y 

<!-- You can add more details here.

-->

#### 5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

<!-- You can choose a brief description here -->
- [x] Unit test
- [ ] Integration test
- [ ] Benchmark (add benchmark stats below)
- [ ] Manual test (add detailed scripts or steps below)
- [ ] Other

<!-- You can add more details here.
None
-->
KCLVM/kclvm/src/api_test.rs

#### 6. Release note

<!-- compatibility change, improvement, bugfix, and new feature need a release note -->

Please refer to [Release Notes Language Style Guide](https://kusionstack.io/docs/governance/release-policy/) to write a quality release note.

```release-note
None
```



Discussion / hints:
****CLA Assistant Lite bot**** All contributors have signed the CLA  ✍️ ✅
I have read the CLA Document and I hereby sign the CLA
## Pull Request Test Coverage Report for [Build 2744948016](https://coveralls.io/builds/51191024)

* **2** of **5523**   **(0.04%)**  changed or added relevant lines in **6** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-7.6%**) to **51.731%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [kclvm/spec/src/main.rs](https://coveralls.io/builds/51191024/source?filename=kclvm%2Fspec%2Fsrc%2Fmain.rs#L2) | 1 | 3 | 33.33%
| [kclvm/spec/src/service/util.rs](https://coveralls.io/builds/51191024/source?filename=kclvm%2Fspec%2Fsrc%2Fservice%2Futil.rs#L2) | 0 | 25 | 0.0%
| [kclvm/spec/src/service/service.rs](https://coveralls.io/builds/51191024/source?filename=kclvm%2Fspec%2Fsrc%2Fservice%2Fservice.rs#L10) | 0 | 109 | 0.0%
| [kclvm/spec/src/service/api.rs](https://coveralls.io/builds/51191024/source?filename=kclvm%2Fspec%2Fsrc%2Fservice%2Fapi.rs#L10) | 0 | 153 | 0.0%
| [kclvm/spec/src/model/gpyrpc.rs](https://coveralls.io/builds/51191024/source?filename=kclvm%2Fspec%2Fsrc%2Fmodel%2Fgpyrpc.rs#L28) | 0 | 5232 | 0.0%
<!-- | **Total:** | **2** | **5523** | **0.04%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/51191024/badge)](https://coveralls.io/builds/51191024) |
| :-- | --: |
| Change from base [Build 2743999583](https://coveralls.io/builds/51186839): |  -7.6% |
| Covered Lines: | 22244 |
| Relevant Lines: | 42999 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Fix patch:
diff --git a/kclvm/parser/src/lib.rs b/kclvm/parser/src/lib.rs
index 04c707bae..9c72492a7 100644
--- a/kclvm/parser/src/lib.rs
+++ b/kclvm/parser/src/lib.rs
@@ -321,6 +321,8 @@ pub struct ModuleCache {
     pub dep_cache: IndexMap<PkgFile, PkgMap>,
     /// File source code
     pub source_code: IndexMap<PathBuf, String>,
+
+    pub last_compile_input: (Vec<String>, Option<LoadProgramOptions>),
 }
 
 impl ModuleCache {
@@ -1022,6 +1024,9 @@ pub fn parse_program(
     })
 }
 
+/// If there are too many files in the directory, it will affect the performance of lsp. Set a maximum number of files
+const MAX_SCAN_FILES: usize = 1000;
+
 /// Parse all kcl files under path and dependencies from opts.
 /// Different from `load_program`, this function will compile files that are not imported.
 pub fn load_all_files_under_paths(
@@ -1047,8 +1052,34 @@ pub fn load_all_files_under_paths(
                 let k_files_from_import = res.paths.clone();
                 let mut paths = paths.to_vec();
                 paths.push(&res.program.root);
+                let module_cache = loader.module_cache.clone();
+                let mut module_cache_write = module_cache.write();
+                match &mut module_cache_write {
+                    Ok(m_cache) => {
+                        let paths: Vec<String> = paths.iter().map(|p| p.to_string()).collect();
+                        let paths_is_cached = paths == m_cache.last_compile_input.0;
+                        let opt_is_cached = match (&m_cache.last_compile_input.1, &opts) {
+                            (None, None) => true,
+                            (Some(old), Some(new)) => old.package_maps == new.package_maps,
+                            _ => false,
+                        };
+                        if paths_is_cached && opt_is_cached {
+                            return Ok(res);
+                        } else {
+                            m_cache.last_compile_input = (paths, opts.clone())
+                        }
+                    }
+                    Err(e) => return Err(anyhow::anyhow!("Parse entry failed: {e}")),
+                }
+                drop(module_cache_write);
+
                 let (k_files_under_path, pkgmap) =
                     get_files_from_path(&res.program.root, &paths, opts)?;
+
+                if k_files_under_path.len() > MAX_SCAN_FILES {
+                    return Ok(res);
+                }
+
                 loader.pkgmap.extend(pkgmap);
 
                 // Filter unparsed file
@@ -1057,12 +1088,20 @@ pub fn load_all_files_under_paths(
                     for p in paths {
                         if !k_files_from_import.contains(p) {
                             let pkgfile = PkgFile::new(p.clone(), pkg.clone());
-                            unparsed_file.push_back(pkgfile);
+                            let module_cache_read = module_cache.read();
+                            match &module_cache_read {
+                                Ok(m_cache) => match m_cache.ast_cache.get(pkgfile.get_path()) {
+                                    Some(_) => continue,
+                                    None => {
+                                        unparsed_file.push_back(pkgfile);
+                                    }
+                                },
+                                _ => {}
+                            }
                         }
                     }
                 }
 
-                let module_cache = loader.module_cache.clone();
                 let pkgs_not_imported = &mut res.program.pkgs_not_imported;
 
                 let mut new_files = HashSet::new();


Test patch:
diff --git a/kclvm/tools/src/LSP/src/test_data/completion_test/unimport/unimport/kcl.mod b/kclvm/tools/src/LSP/src/test_data/completion_test/unimport/unimport/kcl.mod
index c78a893dd..d4dd9b6a8 100644
--- a/kclvm/tools/src/LSP/src/test_data/completion_test/unimport/unimport/kcl.mod
+++ b/kclvm/tools/src/LSP/src/test_data/completion_test/unimport/unimport/kcl.mod
@@ -4,4 +4,4 @@ edition = "v0.9.0"
 version = "0.0.1"
 
 [dependencies]
-pkg = { path = "../pkg" }
\ No newline at end of file
+pkg = { path = "../pkg" }