on master help sign in
Add commands for variable introspection
cc66475Dmitry Sharshakov
helix-term/src/commands.rs+77
@@ -307,6 +307,7 @@ impl Command {
307 dap_start, "Start debug session",
307 dap_start, "Start debug session",
308 dap_run, "Begin program execution",
308 dap_run, "Begin program execution",
309 dap_continue, "Continue program execution",
309 dap_continue, "Continue program execution",
310+ dap_variable_scopes, "List variable scopes",
310 dap_terminate, "End debug session",
311 dap_terminate, "End debug session",
311 suspend, "Suspend"
312 suspend, "Suspend"
312 );
313 );
@@ -1905,6 +1906,49 @@ mod cmd {
1905 Ok(())
1906 Ok(())
1906 }
1907 }
1907
1908
1909+ fn variables(
1910+ cx: &mut compositor::Context,
1911+ args: &[&str],
1912+ _event: PromptEvent,
1913+ ) -> anyhow::Result<()> {
1914+ use helix_lsp::block_on;
1915+
1916+ let scope = args.get(0).and_then(|arg| arg.parse::<usize>().ok());
1917+ if scope.is_none() {
1918+ cx.editor.set_status("Please provide scope ID".to_owned());
1919+ return Ok(());
1920+ }
1921+
1922+ if let Some(debugger) = &mut cx.editor.debugger {
1923+ if debugger.is_running {
1924+ cx.editor
1925+ .set_status("Cannot access variables while target is running".to_owned());
1926+ return Ok(());
1927+ }
1928+ if debugger.stack_pointer.is_none() {
1929+ cx.editor
1930+ .set_status("Cannot find current stack pointer to access variables".to_owned());
1931+ return Ok(());
1932+ }
1933+
1934+ let response = block_on(debugger.variables(scope.unwrap()));
1935+
1936+ let status = match response {
1937+ Ok(vars) => {
1938+ let mut s = String::new();
1939+ for var in vars {
1940+ s.push_str(&format!("{} = {}; ", var.name, var.value));
1941+ }
1942+ s
1943+ }
1944+ Err(_) => "Failed to get variables".to_owned(),
1945+ };
1946+
1947+ cx.editor.set_status(status);
1948+ }
1949+ Ok(())
1950+ }
1951+
1908 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
1952 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
1909 TypableCommand {
1953 TypableCommand {
1910 name: "quit",
1954 name: "quit",
@@ -2143,6 +2187,13 @@ mod cmd {
2143 doc: "Display tree sitter scopes, primarily for theming and development.",
2187 doc: "Display tree sitter scopes, primarily for theming and development.",
2144 fun: tree_sitter_scopes,
2188 fun: tree_sitter_scopes,
2145 completer: None,
2189 completer: None,
2190+ },
2191+ TypableCommand {
2192+ name: "variables",
2193+ alias: None,
2194+ doc: "Display variables of scope. Get scope names using a command in debug menu",
2195+ fun: variables,
2196+ completer: None,
2146 }
2197 }
2147 ];
2198 ];
2148
2199
@@ -4348,6 +4399,32 @@ fn dap_continue(cx: &mut Context) {
4348 }
4399 }
4349}
4400}
4350
4401
4402+fn dap_variable_scopes(cx: &mut Context) {
4403+ use helix_lsp::block_on;
4404+
4405+ if let Some(debugger) = &mut cx.editor.debugger {
4406+ if debugger.is_running {
4407+ cx.editor
4408+ .set_status("Cannot access variables while target is running".to_owned());
4409+ return;
4410+ }
4411+ if debugger.stack_pointer.is_none() {
4412+ cx.editor
4413+ .set_status("Cannot find current stack pointer to access variables".to_owned());
4414+ return;
4415+ }
4416+
4417+ let frame_id = debugger.stack_pointer.clone().unwrap().id;
4418+ let scopes = block_on(debugger.scopes(frame_id)).unwrap();
4419+
4420+ let mut status = String::new();
4421+ for scope in scopes.iter() {
4422+ status.push_str(&format!("{}: {}; ", scope.variables_reference, scope.name))
4423+ }
4424+ cx.editor.set_status(status);
4425+ }
4426+}
4427+
4351fn dap_terminate(cx: &mut Context) {
4428fn dap_terminate(cx: &mut Context) {
4352 use helix_lsp::block_on;
4429 use helix_lsp::block_on;
4353
4430
helix-term/src/keymap.rs+1
@@ -490,6 +490,7 @@ impl Default for Keymaps {
490 "b" => dap_toggle_breakpoint,
490 "b" => dap_toggle_breakpoint,
491 "r" => dap_run,
491 "r" => dap_run,
492 "c" => dap_continue,
492 "c" => dap_continue,
493+ "z" => dap_variable_scopes,
493 "t" => dap_terminate,
494 "t" => dap_terminate,
494 },
495 },
495 "w" => { "Window"
496 "w" => { "Window"