Skip to content

Commit

Permalink
feat: implement custom remote backend
Browse files Browse the repository at this point in the history
  • Loading branch information
panekj committed Feb 25, 2023
1 parent b92c519 commit a256aa9
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 30 deletions.
3 changes: 3 additions & 0 deletions defaults/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ font-size = 0
line-height = 0
shell = ""

[remote]
[remote.entries]

[ui]
font-family = ""
font-size = 13
Expand Down
4 changes: 4 additions & 0 deletions lapce-data/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ pub enum LapceWorkbenchCommand {
#[strum(message = "Connect to SSH Host")]
ConnectSshHost,

#[strum(serialize = "connect_custom_host")]
#[strum(message = "Connect to Custom Host")]
ConnectCustomHost,

#[cfg(windows)]
#[strum(serialize = "connect_wsl")]
#[strum(message = "Connect to WSL")]
Expand Down
28 changes: 28 additions & 0 deletions lapce-data/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,32 @@ pub struct TerminalConfig {
pub shell: String,
}

#[derive(FieldNames, Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct RemoteConfig {
#[field_names(desc = "List of custom remote options to connect to")]
pub entries: HashMap<String, RemoteEntryConfig>,
}

#[derive(FieldNames, Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct RemoteEntryConfig {
#[field_names(desc = "Base executable to use")]
pub program: String,

#[field_names(desc = "Copy file command")]
pub copy_args: Vec<String>,

#[field_names(desc = "Connect command")]
pub exec_args: Vec<String>,

#[field_names(desc = "Start command")]
pub start_args: Option<Vec<String>>,

#[field_names(desc = "Stop command")]
pub stop_args: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct ColorThemeConfig {
Expand Down Expand Up @@ -940,6 +966,7 @@ pub struct LapceConfig {
pub ui: UIConfig,
pub editor: EditorConfig,
pub terminal: TerminalConfig,
pub remote: RemoteConfig,
pub color_theme: ColorThemeConfig,
pub icon_theme: IconThemeConfig,
#[serde(flatten)]
Expand Down Expand Up @@ -1176,6 +1203,7 @@ impl LapceConfig {
}
}
LapceWorkspaceType::RemoteSSH(_) => {}
LapceWorkspaceType::RemoteCustom(_) => {}
#[cfg(windows)]
LapceWorkspaceType::RemoteWSL => {}
}
Expand Down
51 changes: 49 additions & 2 deletions lapce-data/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,13 @@ impl LapceTabData {
Target::Widget(self.palette.widget_id),
));
}
LapceWorkbenchCommand::ConnectCustomHost => {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::RunPalette(Some(PaletteType::CustomHost)),
Target::Widget(self.palette.widget_id),
));
}
#[cfg(windows)]
LapceWorkbenchCommand::ConnectWsl => ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
Expand Down Expand Up @@ -4582,10 +4589,34 @@ impl Display for SshHost {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct CustomHost {
pub name: String,
pub program: String,
pub copy_args: Vec<String>,
pub exec_args: Vec<String>,
pub start_args: Option<Vec<String>>,
pub stop_args: Option<Vec<String>>,
}

impl CustomHost {
pub fn name(&self) -> String {
self.name.clone()
}
}

impl Display for CustomHost {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)?;
Ok(())
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum LapceWorkspaceType {
Local,
RemoteSSH(SshHost),
RemoteCustom(CustomHost),
#[cfg(windows)]
RemoteWSL,
}
Expand All @@ -4595,13 +4626,26 @@ impl LapceWorkspaceType {
pub fn is_remote(&self) -> bool {
matches!(
self,
LapceWorkspaceType::RemoteSSH(_) | LapceWorkspaceType::RemoteWSL
LapceWorkspaceType::RemoteSSH(_)
| LapceWorkspaceType::RemoteCustom(_)
| LapceWorkspaceType::RemoteWSL
)
}

#[cfg(not(windows))]
pub fn is_remote(&self) -> bool {
matches!(self, LapceWorkspaceType::RemoteSSH(_))
matches!(
self,
LapceWorkspaceType::RemoteSSH(_) | LapceWorkspaceType::RemoteCustom(_)
)
}

#[cfg(not(windows))]
pub fn is_remote(&self) -> bool {
matches!(
self,
LapceWorkspaceType::RemoteSSH(_) | LapceWorkspaceType::RemoteCustom(_)
)
}
}

Expand All @@ -4612,6 +4656,9 @@ impl std::fmt::Display for LapceWorkspaceType {
LapceWorkspaceType::RemoteSSH(ssh) => {
write!(f, "ssh://{ssh}")
}
LapceWorkspaceType::RemoteCustom(custom) => {
write!(f, "{custom}")
}
#[cfg(windows)]
LapceWorkspaceType::RemoteWSL => f.write_str("WSL"),
}
Expand Down
56 changes: 53 additions & 3 deletions lapce-data/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
},
config::LapceConfig,
data::{
FocusArea, LapceMainSplitData, LapceTabData, LapceWorkspace,
CustomHost, FocusArea, LapceMainSplitData, LapceTabData, LapceWorkspace,
LapceWorkspaceType, SshHost,
},
db::LapceDb,
Expand Down Expand Up @@ -61,6 +61,7 @@ pub enum PaletteType {
ColorTheme,
IconTheme,
SshHost,
CustomHost,
Language,
}

Expand All @@ -78,6 +79,7 @@ impl PaletteType {
| PaletteType::ColorTheme
| PaletteType::IconTheme
| PaletteType::SshHost
| PaletteType::CustomHost
| PaletteType::Language => "".to_string(),
}
}
Expand All @@ -99,6 +101,7 @@ impl PaletteType {
match current_type {
PaletteType::Reference
| PaletteType::SshHost
| PaletteType::CustomHost
| PaletteType::ColorTheme
| PaletteType::IconTheme
| PaletteType::Language => {
Expand Down Expand Up @@ -155,6 +158,7 @@ pub enum PaletteItemContent {
ReferenceLocation(PathBuf, EditorLocation<Position>),
Workspace(LapceWorkspace),
SshHost(SshHost),
CustomHost(CustomHost),
Command(LapceCommand),
ColorTheme(String),
IconTheme(String),
Expand Down Expand Up @@ -305,6 +309,19 @@ impl PaletteItemContent {
));
}
}
PaletteItemContent::CustomHost(custom) => {
if !preview {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::SetWorkspace(LapceWorkspace {
kind: LapceWorkspaceType::RemoteCustom(custom.clone()),
path: None,
last_open: 0,
}),
Target::Auto,
));
}
}
}
true
}
Expand Down Expand Up @@ -521,7 +538,8 @@ impl PaletteData {
| PaletteType::ColorTheme
| PaletteType::IconTheme
| PaletteType::Language
| PaletteType::SshHost => &self.input,
| PaletteType::SshHost
| PaletteType::CustomHost => &self.input,
PaletteType::Line
| PaletteType::DocumentSymbol
| PaletteType::WorkspaceSymbol
Expand Down Expand Up @@ -659,6 +677,9 @@ impl PaletteViewData {
PaletteType::SshHost => {
self.get_ssh_hosts(ctx);
}
PaletteType::CustomHost => {
self.get_custom_hosts(ctx);
}
PaletteType::GlobalSearch => {
self.get_global_search(ctx);
}
Expand Down Expand Up @@ -721,7 +742,8 @@ impl PaletteViewData {
| PaletteType::ColorTheme
| PaletteType::IconTheme
| PaletteType::Language
| PaletteType::SshHost => 0,
| PaletteType::SshHost
| PaletteType::CustomHost => 0,
PaletteType::Line
| PaletteType::DocumentSymbol
| PaletteType::WorkspaceSymbol
Expand Down Expand Up @@ -927,6 +949,31 @@ impl PaletteViewData {
.collect();
}

fn get_custom_hosts(&mut self, _ctx: &mut EventCtx) {
let mut hosts = HashSet::new();
for (name, entry) in &self.config.remote.entries {
hosts.insert(CustomHost {
name: name.clone(),
program: entry.program.clone(),
copy_args: entry.copy_args.clone(),
exec_args: entry.exec_args.clone(),
start_args: entry.start_args.clone(),
stop_args: entry.stop_args.clone(),
});
}

let palette = Arc::make_mut(&mut self.palette);
palette.total_items = hosts
.iter()
.map(|custom| PaletteItem {
content: PaletteItemContent::CustomHost(custom.clone()),
filter_text: custom.to_string(),
score: 0,
indices: vec![],
})
.collect();
}

fn get_workspaces(&mut self, _ctx: &mut EventCtx) {
let workspaces = self.db.recent_workspaces().unwrap_or_default();
let palette = Arc::make_mut(&mut self.palette);
Expand All @@ -939,6 +986,9 @@ impl PaletteViewData {
LapceWorkspaceType::RemoteSSH(ssh) => {
format!("[{ssh}] {text}")
}
LapceWorkspaceType::RemoteCustom(custom) => {
format!("[{custom}] {text}")
}
#[cfg(windows)]
LapceWorkspaceType::RemoteWSL => {
format!("[wsl] {text}")
Expand Down
Loading

0 comments on commit a256aa9

Please sign in to comment.