Skip to content

Commit 629675f

Browse files
committed
fix launching neovim with line number not working
1 parent 143d5f3 commit 629675f

7 files changed

Lines changed: 83 additions & 49 deletions

File tree

crates/bridge/src/launcher.rs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const ERR_TERMINAL_NOT_FOUND: &str = "Could not find any suitable terminal";
1919

2020
const VAR_CLASSNAME: &str = "{CLASSNAME}";
2121
const VAR_ADDRESS: &str = "{ADDR}";
22-
const VAR_REMOTE_CMD: &str = "{REMOTE_CMD}";
22+
const VAR_LINE: &str = "{LINE}";
23+
const VAR_FILE: &str = "{FILE}";
2324

2425
#[derive(Debug)]
2526
struct Launcher(PathBuf, Vec<String>);
@@ -106,7 +107,8 @@ fn create_launcher(cfg: &PluginConfig, nvim: &String) -> Result<Launcher> {
106107
args.push(VAR_ADDRESS.to_string());
107108

108109
args.push("--remote".to_string());
109-
args.push(VAR_REMOTE_CMD.to_string());
110+
args.push(VAR_LINE.to_string());
111+
args.push(VAR_FILE.to_string());
110112

111113
Ok(Launcher(executable.clone(), args))
112114
}
@@ -151,7 +153,8 @@ fn create_launcher(cfg: &PluginConfig, nvim: &String) -> Result<Launcher> {
151153
args.push(VAR_ADDRESS.to_string());
152154

153155
args.push("--remote".to_string());
154-
args.push(VAR_REMOTE_CMD.to_string());
156+
args.push(VAR_LINE.to_string());
157+
args.push(VAR_FILE.to_string());
155158

156159
Some(Launcher(exe, args))
157160
} else {
@@ -194,7 +197,8 @@ fn create_launcher(cfg: &PluginConfig, nvim: &String) -> Result<Launcher> {
194197
args.push(VAR_ADDRESS.to_string());
195198

196199
args.push("--remote".to_string());
197-
args.push(VAR_REMOTE_CMD.to_string());
200+
args.push(VAR_LINE.to_string());
201+
args.push(VAR_FILE.to_string());
198202

199203
return Some(Launcher(path, args));
200204
}
@@ -223,7 +227,8 @@ fn create_launcher(cfg: &PluginConfig, nvim: &String) -> Result<Launcher> {
223227
args.push(VAR_ADDRESS.to_string());
224228

225229
args.push("--remote".to_string());
226-
args.push(VAR_REMOTE_CMD.to_string());
230+
args.push(VAR_LINE.to_string());
231+
args.push(VAR_FILE.to_string());
227232

228233
return Some(Launcher(path, args));
229234
}
@@ -260,7 +265,13 @@ fn create_launcher(cfg: &PluginConfig, nvim: &String) -> Result<Launcher> {
260265
}
261266
}
262267

263-
fn nvim_open_file_remote(nvim: &str, server: &str, remote_cmd: &str) -> Result<()> {
268+
fn nvim_open_file_remote(nvim: &str, server: &str, file: &str, line: Option<usize>) -> Result<()> {
269+
let remote_cmd = if let Some(line) = line {
270+
format!("+{line} {file}")
271+
} else {
272+
file.to_string()
273+
};
274+
264275
tracing::debug!("Open '{remote_cmd}' via socket: {server}");
265276

266277
let out = Command::new(nvim)
@@ -277,7 +288,13 @@ fn nvim_open_file_remote(nvim: &str, server: &str, remote_cmd: &str) -> Result<(
277288
Ok(())
278289
}
279290

280-
fn run_fsock(launcher: Launcher, nvim: &str, root_dir: &Path, remote_cmd: &str) -> Result<()> {
291+
fn run_fsock(
292+
launcher: Launcher,
293+
nvim: &str,
294+
root_dir: &Path,
295+
file: &str,
296+
line: Option<usize>,
297+
) -> Result<()> {
281298
let socket_file = utils::runtime_dir(
282299
root_dir
283300
.to_str()
@@ -302,7 +319,8 @@ fn run_fsock(launcher: Launcher, nvim: &str, root_dir: &Path, remote_cmd: &str)
302319
socket_file
303320
.to_str()
304321
.context("could not convert path to string")?,
305-
remote_cmd,
322+
file,
323+
line,
306324
) {
307325
tracing::error!("Failed to communicate with neovim server: {err:?}");
308326

@@ -317,7 +335,13 @@ fn run_fsock(launcher: Launcher, nvim: &str, root_dir: &Path, remote_cmd: &str)
317335
Ok(())
318336
}
319337

320-
fn run_netsock(launcher: Launcher, nvim: &str, root_dir: &Path, remote_cmd: &str) -> Result<()> {
338+
fn run_netsock(
339+
launcher: Launcher,
340+
nvim: &str,
341+
root_dir: &Path,
342+
file: &str,
343+
line: Option<usize>,
344+
) -> Result<()> {
321345
let port_file = utils::runtime_dir(
322346
root_dir
323347
.to_str()
@@ -338,7 +362,7 @@ fn run_netsock(launcher: Launcher, nvim: &str, root_dir: &Path, remote_cmd: &str
338362
if is_port_in_use(port) {
339363
// if we couldnt communicate with the server despite existing apparently
340364
// delete it and start a new instance
341-
if let Err(err) = nvim_open_file_remote(nvim, &socket, remote_cmd) {
365+
if let Err(err) = nvim_open_file_remote(nvim, &socket, file, line) {
342366
tracing::error!("Failed to communicate with neovim server: {err:?}");
343367

344368
let new_port = utils::find_free_port();
@@ -357,7 +381,7 @@ fn run_netsock(launcher: Launcher, nvim: &str, root_dir: &Path, remote_cmd: &str
357381
}
358382

359383
pub fn run(
360-
plugin_config: PluginConfig,
384+
plugin_config: &PluginConfig,
361385
root_dir: PathBuf,
362386
file: &Path,
363387
line: Option<usize>,
@@ -367,7 +391,7 @@ pub fn run(
367391
.context("could not convert nvim path to string")?
368392
.to_string();
369393

370-
let launcher = create_launcher(&plugin_config, &nvim)?;
394+
let launcher = create_launcher(plugin_config, &nvim)?;
371395

372396
let launcher = if cfg!(target_os = "linux") {
373397
launcher.apply_var(
@@ -390,21 +414,22 @@ pub fn run(
390414

391415
let file_str = file.to_str().context("could not convert path to string")?;
392416

393-
let remote_cmd = match line {
394-
Some(line) => format!("+{line} {file_str}"),
395-
None => file_str.to_string(),
417+
let launcher = if let Some(line) = line {
418+
launcher.apply_var(VAR_LINE, &format!("+{line}"))
419+
} else {
420+
launcher
396421
};
397422

398-
let launcher = launcher.apply_var(VAR_REMOTE_CMD, &remote_cmd.clone());
423+
let launcher = launcher.apply_var(VAR_FILE, file_str);
399424

400425
match plugin_config.socket_type {
401-
Some(SocketType::Fsock) => run_fsock(launcher, &nvim, &root_dir, &remote_cmd)?,
402-
Some(SocketType::Netsock) => run_netsock(launcher, &nvim, &root_dir, &remote_cmd)?,
426+
Some(SocketType::Fsock) => run_fsock(launcher, &nvim, &root_dir, file_str, line)?,
427+
Some(SocketType::Netsock) => run_netsock(launcher, &nvim, &root_dir, file_str, line)?,
403428
None => {
404429
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
405-
run_fsock(launcher, &nvim, &root_dir, &remote_cmd)?;
430+
run_fsock(launcher, &nvim, &root_dir, file_str, line)?;
406431
} else {
407-
run_netsock(launcher, &nvim, &root_dir, &remote_cmd)?;
432+
run_netsock(launcher, &nvim, &root_dir, file_str, line)?;
408433
}
409434
}
410435
}

crates/bridge/src/main.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ enum Commands {
4141
#[arg(long = "executable")]
4242
executable: Option<String>,
4343

44-
#[arg(long = "extra-arguments", value_delimiter = ' ', num_args = 0..)]
45-
extra_arguments: Option<Vec<String>>,
46-
4744
#[arg(long = "terminal-class-argument")]
4845
terminal_class_argument: Option<String>,
4946

5047
#[arg(long = "terminal-run-argument")]
5148
terminal_run_argument: Option<String>,
5249

53-
#[clap(value_name = "GAME_ROOT_DIR", index = 1)]
50+
#[clap(value_name = "GAME_ROOT_DIR")]
5451
game_root_dir: String,
5552

56-
#[clap(value_name = "FILE", index = 2)]
53+
#[clap(value_name = "FILE")]
5754
file: String,
5855

59-
#[clap(value_name = "LINE", index = 3)]
56+
#[clap(value_name = "LINE")]
6057
line: Option<usize>,
58+
59+
#[arg(last = true)]
60+
extra_arguments: Option<Vec<String>>,
6161
},
6262
/// Focus the currently open instance of Neovim
6363
FocusNeovim {
@@ -126,10 +126,10 @@ fn main() -> Result<()> {
126126
tracing_subscriber::fmt()
127127
.with_file(true)
128128
.with_line_number(true)
129-
.with_max_level(if args.as_ref().is_some_and(|args| args.debug) {
130-
Level::DEBUG
131-
} else {
132-
Level::INFO
129+
.with_max_level(match &args {
130+
Some(args) if args.debug => Level::DEBUG,
131+
Some(_) => Level::INFO,
132+
None => Level::DEBUG,
133133
})
134134
.with_writer(logfile)
135135
.with_ansi(false)
@@ -158,7 +158,7 @@ fn main() -> Result<()> {
158158
file,
159159
line,
160160
} => launcher::run(
161-
PluginConfig {
161+
&PluginConfig {
162162
launcher_type,
163163
socket_type,
164164
executable,

crates/core/assets/run_linux.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
{BRIDGE_PATH} {DEBUG_FLAG} launch-neovim {LAUNCH_ARGS} "$(realpath .)" "$1" $2
2+
{BRIDGE_PATH} {DEBUG_FLAG} launch-neovim {LAUNCH_PRE_ARGS} "$(realpath .)" "$1" $2 {LAUNCH_POST_ARGS}

crates/core/assets/run_macos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22
export PATH="/usr/bin:/usr/local/bin:$PATH"
3-
{BRIDGE_PATH} {DEBUG_FLAG} launch-neovim {LAUNCH_ARGS} "$(realpath .)" "$1" $2
3+
{BRIDGE_PATH} {DEBUG_FLAG} launch-neovim {LAUNCH_PRE_ARGS} "$(realpath .)" "$1" $2 {LAUNCH_POST_ARGS}

crates/core/assets/run_windows.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@echo off
2-
{BRIDGE_PATH} {DEBUG_FLAG} launch-neovim {LAUNCH_ARGS} "%CD%" "%~1" %2
2+
{BRIDGE_PATH} {DEBUG_FLAG} launch-neovim {LAUNCH_PRE_ARGS} "%CD%" "%~1" %2 {LAUNCH_POST_ARGS}

crates/core/src/editor_config.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct LauncherSettings {
4646

4747
impl LauncherSettings {
4848
#[must_use]
49-
pub fn bridge_cli_args(&self) -> Vec<String> {
49+
pub fn bridge_pre_cli_args(&self) -> Vec<String> {
5050
let mut args = Vec::new();
5151

5252
args.push("--launcher-type".to_string());
@@ -68,13 +68,6 @@ impl LauncherSettings {
6868
args.push(executable.clone());
6969
}
7070

71-
if let Some(extra_args) = &self.extra_arguments {
72-
args.push("--extra-arguments".to_string());
73-
for arg in extra_args {
74-
args.push(arg.clone());
75-
}
76-
}
77-
7871
if let Some(terminal) = &self.terminal {
7972
if let Some(class_arg) = &terminal.class_argument {
8073
args.push("--terminal-class-argument".to_string());
@@ -89,6 +82,20 @@ impl LauncherSettings {
8982

9083
args
9184
}
85+
86+
#[must_use]
87+
pub fn bridge_post_cli_args(&self) -> Vec<String> {
88+
let mut args = Vec::new();
89+
90+
if let Some(extra_args) = &self.extra_arguments {
91+
args.push("--".to_string());
92+
for arg in extra_args {
93+
args.push(arg.clone());
94+
}
95+
}
96+
97+
args
98+
}
9299
}
93100

94101
#[cfg(target_os = "linux")]
@@ -118,7 +125,8 @@ fn create_runner_script(
118125

119126
let script_path = dir.join(format!("run.{SCRIPT_EXT}"));
120127
let bridge_path = bridge::path(plugin_root)?;
121-
let launch_args = launcher_settings.bridge_cli_args().join(" ");
128+
let launch_pre_args = launcher_settings.bridge_pre_cli_args().join(" ");
129+
let launch_post_args = launcher_settings.bridge_post_cli_args().join(" ");
122130

123131
fs::write(
124132
&script_path,
@@ -129,7 +137,7 @@ fn create_runner_script(
129137
.to_str()
130138
.context("could not convert bridge path")?,
131139
)
132-
.replace("{LAUNCH_ARGS}", &launch_args)
140+
.replace("{LAUNCH_PRE_ARGS}", &launch_pre_args)
133141
.replace(
134142
"{DEBUG_FLAG}",
135143
if let Some(debug) = launcher_settings.debug
@@ -139,7 +147,8 @@ fn create_runner_script(
139147
} else {
140148
""
141149
},
142-
),
150+
)
151+
.replace("{LAUNCH_POST_ARGS}", &launch_post_args),
143152
)?;
144153

145154
#[cfg(not(target_os = "windows"))]

crates/core/src/script_api.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn compile_table(out: &mut String, table: &Value, parents: &Vec<&Value>) -> Resu
161161
table.name.clone().expect("table must have name")
162162
)?;
163163
} else {
164-
writeln!(out, "{} = {{}}", table.fully_qualified_name(&parents)?)?;
164+
writeln!(out, "{} = {{}}", table.fully_qualified_name(parents)?)?;
165165
}
166166

167167
if let Some(members) = &table.members {
@@ -299,17 +299,17 @@ fn compile_function(out: &mut String, function: &Value, parents: &Vec<&Value>) -
299299
String::new()
300300
};
301301

302-
if !parents.is_empty() {
302+
if parents.is_empty() {
303303
writeln!(
304304
out,
305305
"function {}({params}) end",
306-
function.fully_qualified_name(&parents)?,
306+
function.name.clone().unwrap()
307307
)?;
308308
} else {
309309
writeln!(
310310
out,
311311
"function {}({params}) end",
312-
function.name.clone().unwrap()
312+
function.fully_qualified_name(parents)?,
313313
)?;
314314
}
315315

0 commit comments

Comments
 (0)