Skip to content

Commit 1656d7e

Browse files
committed
refactor status bar
1 parent 0a08c9e commit 1656d7e

File tree

11 files changed

+215
-176
lines changed

11 files changed

+215
-176
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use lsp_types::ClientInfo;
2+
3+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
4+
pub enum ClientId {
5+
VSCode,
6+
Intellij,
7+
Neovim,
8+
Other,
9+
}
10+
11+
impl Default for ClientId {
12+
fn default() -> Self {
13+
ClientId::Other
14+
}
15+
}
16+
17+
#[allow(unused)]
18+
impl ClientId {
19+
pub fn is_vscode(&self) -> bool {
20+
matches!(self, ClientId::VSCode)
21+
}
22+
23+
pub fn is_intellij(&self) -> bool {
24+
matches!(self, ClientId::Intellij)
25+
}
26+
27+
pub fn is_neovim(&self) -> bool {
28+
matches!(self, ClientId::Neovim)
29+
}
30+
31+
pub fn is_other(&self) -> bool {
32+
matches!(self, ClientId::Other)
33+
}
34+
}
35+
36+
pub fn get_client_id(client_info: &Option<ClientInfo>) -> ClientId {
37+
match client_info {
38+
Some(info) => {
39+
if info.name == "Visual Studio Code" {
40+
ClientId::VSCode
41+
} else if info.name == "IntelliJ" {
42+
ClientId::Intellij
43+
} else if info.name == "Neovim" {
44+
ClientId::Neovim
45+
} else {
46+
ClientId::Other
47+
}
48+
}
49+
None => ClientId::Other,
50+
}
51+
}

crates/emmylua_ls/src/context/config_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{path::PathBuf, sync::Arc, time::Duration};
22

3-
use super::{ClientProxy, VsCodeStatusBar};
3+
use super::{ClientProxy, StatusBar};
44
use crate::handlers::{init_analysis, ClientConfig};
55
use code_analysis::{load_configs, EmmyLuaAnalysis, Emmyrc};
66
use emmylua_codestyle::update_code_style;
@@ -14,7 +14,7 @@ use tokio_util::sync::CancellationToken;
1414
pub struct ConfigManager {
1515
analysis: Arc<RwLock<EmmyLuaAnalysis>>,
1616
client: Arc<ClientProxy>,
17-
status_bar: Arc<VsCodeStatusBar>,
17+
status_bar: Arc<StatusBar>,
1818
pub client_config: ClientConfig,
1919
pub workspace_folders: Vec<PathBuf>,
2020
config_update_token: Arc<Mutex<Option<CancellationToken>>>,
@@ -25,7 +25,7 @@ impl ConfigManager {
2525
pub fn new(
2626
analysis: Arc<RwLock<EmmyLuaAnalysis>>,
2727
client: Arc<ClientProxy>,
28-
status_bar: Arc<VsCodeStatusBar>,
28+
status_bar: Arc<StatusBar>,
2929
) -> Self {
3030
Self {
3131
analysis,

crates/emmylua_ls/src/context/file_diagnostic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ use log::{debug, info};
55
use tokio::sync::{Mutex, RwLock};
66
use tokio_util::sync::CancellationToken;
77

8-
use super::{status_bar::VsCodeStatusBar, ClientProxy};
8+
use super::{status_bar::StatusBar, ClientProxy};
99

1010
pub struct FileDiagnostic {
1111
analysis: Arc<RwLock<EmmyLuaAnalysis>>,
1212
client: Arc<ClientProxy>,
1313
#[allow(unused)]
14-
status_bar: Arc<VsCodeStatusBar>,
14+
status_bar: Arc<StatusBar>,
1515
diagnostic_tokens: Arc<Mutex<HashMap<FileId, CancellationToken>>>,
1616
}
1717

1818
impl FileDiagnostic {
1919
pub fn new(
2020
analysis: Arc<RwLock<EmmyLuaAnalysis>>,
2121
client: Arc<ClientProxy>,
22-
status_bar: Arc<VsCodeStatusBar>,
22+
status_bar: Arc<StatusBar>,
2323
) -> Self {
2424
Self {
2525
analysis,

crates/emmylua_ls/src/context/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
mod client;
2+
mod client_id;
23
mod config_manager;
34
mod file_diagnostic;
45
mod snapshot;
56
mod status_bar;
67

78
pub use client::ClientProxy;
9+
pub use client_id::{get_client_id, ClientId};
810
use code_analysis::EmmyLuaAnalysis;
911
pub use config_manager::load_emmy_config;
1012
pub use config_manager::ConfigManager;
1113
pub use file_diagnostic::FileDiagnostic;
1214
use lsp_server::{Connection, ErrorCode, Message, RequestId, Response};
1315
pub use snapshot::ServerContextSnapshot;
14-
pub use status_bar::VsCodeStatusBar;
15-
pub use status_bar::Task;
16+
pub use status_bar::StatusBar;
17+
pub use status_bar::ProgressTask;
1618
use std::{collections::HashMap, future::Future, sync::Arc};
1719
use tokio::sync::{Mutex, RwLock};
1820
use tokio_util::sync::CancellationToken;
@@ -25,7 +27,7 @@ pub struct ServerContext {
2527
cancllations: Arc<Mutex<HashMap<RequestId, CancellationToken>>>,
2628
file_diagnostic: Arc<FileDiagnostic>,
2729
config_manager: Arc<RwLock<ConfigManager>>,
28-
status_bar: Arc<VsCodeStatusBar>,
30+
status_bar: Arc<StatusBar>,
2931
}
3032

3133
impl ServerContext {
@@ -39,7 +41,7 @@ impl ServerContext {
3941
analysis.init_std_lib();
4042

4143
let analysis = Arc::new(RwLock::new(analysis));
42-
let status_bar = Arc::new(VsCodeStatusBar::new(client.clone()));
44+
let status_bar = Arc::new(StatusBar::new(client.clone()));
4345
let file_diagnostic = Arc::new(FileDiagnostic::new(
4446
analysis.clone(),
4547
client.clone(),

crates/emmylua_ls/src/context/snapshot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use code_analysis::EmmyLuaAnalysis;
55

66
use super::{
77
client::ClientProxy, config_manager::ConfigManager, file_diagnostic::FileDiagnostic,
8-
status_bar::VsCodeStatusBar,
8+
status_bar::StatusBar,
99
};
1010

1111
#[derive(Clone)]
@@ -14,5 +14,5 @@ pub struct ServerContextSnapshot {
1414
pub client: Arc<ClientProxy>,
1515
pub file_diagnostic: Arc<FileDiagnostic>,
1616
pub config_manager: Arc<RwLock<ConfigManager>>,
17-
pub status_bar: Arc<VsCodeStatusBar>,
17+
pub status_bar: Arc<StatusBar>,
1818
}

crates/emmylua_ls/src/context/status_bar.rs

Lines changed: 103 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,93 +6,135 @@ use lsp_types::{
66
};
77
use serde::{Deserialize, Serialize};
88

9-
use super::ClientProxy;
9+
use super::{ClientId, ClientProxy};
1010

11-
pub struct VsCodeStatusBar {
11+
pub struct StatusBar {
1212
client: Arc<ClientProxy>,
1313
}
1414

1515
#[derive(Clone, Copy)]
16-
pub enum Task {
16+
pub enum ProgressTask {
1717
LoadWorkspace = 0,
1818
DiagnoseWorkspace = 1,
1919
}
2020

21-
fn get_task_name(task: &Task) -> &'static str {
22-
match task {
23-
Task::LoadWorkspace => "Load workspace",
24-
Task::DiagnoseWorkspace => "Diagnose workspace",
21+
impl ProgressTask {
22+
pub fn as_i32(&self) -> i32 {
23+
*self as i32
24+
}
25+
26+
pub fn get_task_name(&self) -> &'static str {
27+
match self {
28+
ProgressTask::LoadWorkspace => "Load workspace",
29+
ProgressTask::DiagnoseWorkspace => "Diagnose workspace",
30+
}
2531
}
2632
}
2733

28-
impl VsCodeStatusBar {
34+
impl StatusBar {
2935
pub fn new(client: Arc<ClientProxy>) -> Self {
3036
Self { client }
3137
}
3238

33-
pub fn set_server_status(&self, health: &str, loading: bool, message: &str) {
34-
self.client.send_notification(
35-
"emmy/setServerStatus",
36-
EmmyServerStatus {
37-
health: health.to_string(),
38-
loading,
39-
message: message.to_string(),
40-
},
41-
);
39+
pub fn create_progress_task(&self, client_id: ClientId, task: ProgressTask) {
40+
match client_id {
41+
ClientId::VSCode => {
42+
self.vscode_set_server_status("ok", true, task.get_task_name());
43+
}
44+
_ => {
45+
self.client.send_notification(
46+
"window/workDoneProgress/create",
47+
WorkDoneProgressCreateParams {
48+
token: NumberOrString::Number(task.as_i32()),
49+
},
50+
);
51+
self.client.send_notification(
52+
"$/progress",
53+
ProgressParams {
54+
token: NumberOrString::Number(task as i32),
55+
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(
56+
WorkDoneProgressBegin {
57+
title: task.get_task_name().to_string(),
58+
cancellable: Some(false),
59+
message: Some(task.get_task_name().to_string()),
60+
percentage: None,
61+
},
62+
)),
63+
},
64+
)
65+
}
66+
}
4267
}
4368

44-
pub fn start_task(&self, task: Task) {
45-
self.client.send_notification(
46-
"window/workDoneProgress/create",
47-
WorkDoneProgressCreateParams {
48-
token: NumberOrString::Number(task as i32),
49-
},
50-
);
51-
self.client.send_notification(
52-
"$/progress",
53-
ProgressParams {
54-
token: NumberOrString::Number(task as i32),
55-
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(
56-
WorkDoneProgressBegin {
57-
title: get_task_name(&task).to_string(),
58-
cancellable: Some(false),
59-
message: Some(get_task_name(&task).to_string()),
60-
percentage: None,
61-
},
62-
)),
63-
},
64-
)
69+
pub fn update_progress_task(
70+
&self,
71+
client_id: ClientId,
72+
task: ProgressTask,
73+
percentage: Option<u32>,
74+
message: Option<String>,
75+
) {
76+
match client_id {
77+
ClientId::VSCode => {
78+
if let Some(message) = message {
79+
self.vscode_report_progress(&message, percentage.unwrap_or(0) as f64 / 100.0);
80+
} else {
81+
self.vscode_report_progress(task.get_task_name(), percentage.unwrap() as f64);
82+
}
83+
}
84+
_ => self.client.send_notification(
85+
"$/progress",
86+
ProgressParams {
87+
token: NumberOrString::Number(task.as_i32()),
88+
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Report(
89+
WorkDoneProgressReport {
90+
percentage,
91+
cancellable: Some(false),
92+
message,
93+
},
94+
)),
95+
},
96+
),
97+
}
6598
}
6699

67-
pub fn update_task(&self, task: Task, percentage: Option<u32>, message: Option<String>) {
68-
self.client.send_notification(
69-
"$/progress",
70-
ProgressParams {
71-
token: NumberOrString::Number(task as i32),
72-
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Report(
73-
WorkDoneProgressReport {
74-
percentage,
75-
cancellable: Some(false),
76-
message,
77-
},
78-
)),
79-
},
80-
)
100+
pub fn finish_progress_task(
101+
&self,
102+
client_id: ClientId,
103+
task: ProgressTask,
104+
message: Option<String>,
105+
) {
106+
match client_id {
107+
ClientId::VSCode => {
108+
if let Some(message) = message {
109+
self.vscode_set_server_status("ok", false, &message);
110+
} else {
111+
self.vscode_set_server_status("ok", false, task.get_task_name());
112+
}
113+
}
114+
_ => self.client.send_notification(
115+
"$/progress",
116+
ProgressParams {
117+
token: NumberOrString::Number(task.as_i32()),
118+
value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(
119+
WorkDoneProgressEnd { message },
120+
)),
121+
},
122+
),
123+
}
81124
}
82125

83-
pub fn finish_task(&self, task: Task, message: Option<String>) {
126+
fn vscode_set_server_status(&self, health: &str, loading: bool, message: &str) {
84127
self.client.send_notification(
85-
"$/progress",
86-
ProgressParams {
87-
token: NumberOrString::Number(task as i32),
88-
value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(WorkDoneProgressEnd {
89-
message,
90-
})),
128+
"emmy/setServerStatus",
129+
EmmyServerStatus {
130+
health: health.to_string(),
131+
loading,
132+
message: message.to_string(),
91133
},
92-
)
134+
);
93135
}
94136

95-
pub fn report_progress(&self, message: &str, percentage: f64) {
137+
fn vscode_report_progress(&self, message: &str, percentage: f64) {
96138
self.client.send_notification(
97139
"emmy/progressReport",
98140
EmmyProgress {

crates/emmylua_ls/src/handlers/code_lens/resolve_code_lens.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use code_analysis::LuaCompilation;
22
use lsp_types::{CodeLens, Command, Location, Range, Uri};
33

4-
use crate::handlers::{
5-
initialized::ClientId,
6-
references::{search_decl_references, search_member_references},
4+
use crate::{
5+
context::ClientId,
6+
handlers::references::{search_decl_references, search_member_references},
77
};
88

99
use super::CodeLensData;

0 commit comments

Comments
 (0)