Skip to content

Commit 7fdcfef

Browse files
committed
feat(sourcekit): clear state on .compile change
Doesn't seem critical now that the sourcekit lsp server is reloaded on compile.
1 parent ef92912 commit 7fdcfef

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ call dein#add('tami5/xbase', { 'build': 'make install' })
137137
lua require'xbase'.setup()
138138
```
139139

140-
**NOTE:** You need to setup sourcekit-lsp (see [sourcekit-setup])
140+
> **NOTE:** You need to setup sourcekit-lsp (see [sourcekit-setup]) and consider adding more
141+
> file to root patterns
141142
142143
## 🎮 Usage
143144

@@ -151,7 +152,7 @@ TLDR:
151152

152153
When you start a neovim instance with a root that contains `project.yml,` `Project.swift,` or
153154
`*.xcodeproj,` the daemon server will auto-start if no instance is running, and register the
154-
project once for recompile-watch. To communicate with your deamon, checkout the configurable
155+
project once for recompile-watch. To communicate with your daemon, checkout the configurable
155156
shortcuts.
156157

157158
### Statusline
@@ -163,6 +164,7 @@ using `vim.g.xbase_watch_build_status` you can easily setup statusline indicator
163164
require("xbase.util").feline_provider() -- append to feline setup function
164165
```
165166

167+
166168
## ⚙️ Defaults
167169
```lua
168170
-- NOTE: Defaults

lua/xbase/util.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ M.get_targets_runners = function(project)
3636
end
3737

3838
M.reload_lsp_servers = function()
39-
local clients = require("lspconfig.util").get_managed_clients()
40-
local ids = ""
41-
for _, client in ipairs(clients) do
42-
if client.name == "sourcekit" then
43-
ids = ids .. client.id
44-
end
45-
end
46-
47-
print "Restarting Sourcekit Server"
48-
vim.cmd("LspRestart " .. ids)
39+
-- local clients = require("lspconfig.util").get_managed_clients()
40+
-- local ids = ""
41+
-- for _, client in ipairs(clients) do
42+
-- if client.name == "sourcekit" then
43+
-- ids = ids .. client.id
44+
-- end
45+
-- end
46+
47+
print "Restarting Servers"
48+
vim.cmd "LspRestart"
4949
end
5050

5151
M.is_watching = function(config, command, device)

sourcekit/src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use once_cell::sync::OnceCell;
77
use serde_json::{json, Value};
88
use std::path::Path;
99
use std::sync::Mutex;
10+
use std::time::SystemTime;
1011
use std::{collections::HashMap, path::PathBuf};
1112
use xclog::{XCCompilationDatabase, XCCompileArgs, XCCompileCommand};
1213
mod extensions;
@@ -29,6 +30,8 @@ pub struct State {
2930
compile_db: XCCompilationDatabase,
3031
file_args: HashMap<PathBuf, XCCompileArgs>,
3132
root_path: PathBuf,
33+
compile_filepath: PathBuf,
34+
last_modified: SystemTime,
3235
}
3336

3437
fn state() -> &'static Mutex<State> {
@@ -43,7 +46,10 @@ fn initialize(params: &InitializeBuild) -> Result<InitializeBuild> {
4346
let compile_filepath = get_compile_filepath(root_uri).unwrap();
4447
let cache_path = get_build_cache_dir(&root_path)?;
4548
let index_store_path = get_index_store_path(&cache_path, &config_filepath);
46-
let compile_db = XCCompilationDatabase::try_from_filepath(compile_filepath)?;
49+
let compile_db = XCCompilationDatabase::try_from_filepath(&compile_filepath)?;
50+
51+
let attr = std::fs::metadata(&compile_filepath)?;
52+
let last_modified = attr.modified()?;
4753

4854
let response = InitializeBuild::new(
4955
SERVER_NAME,
@@ -62,7 +68,9 @@ fn initialize(params: &InitializeBuild) -> Result<InitializeBuild> {
6268
.set(Mutex::new(State {
6369
root_path,
6470
file_args: Default::default(),
71+
compile_filepath,
6572
compile_db,
73+
last_modified,
6674
}))
6775
.unwrap();
6876
Ok(response)
@@ -71,6 +79,12 @@ fn initialize(params: &InitializeBuild) -> Result<InitializeBuild> {
7179
fn get_compile_args<'a>(path: impl AsRef<Path>) -> Result<XCCompileArgs> {
7280
let mut state = state().lock().unwrap();
7381
let path = path.as_ref();
82+
83+
if state.last_modified != std::fs::metadata(&state.compile_filepath)?.modified()? {
84+
state.compile_db = XCCompilationDatabase::try_from_filepath(&state.compile_filepath)?;
85+
state.file_args = Default::default();
86+
}
87+
7488
if state.file_args.contains_key(path) {
7589
log::debug!("Using Cached file args ...");
7690
state.file_args.get(path)

0 commit comments

Comments
 (0)