debugger: Improve Go support (#31559)
Supersedes https://github.com/zed-industries/zed/pull/31345 This PR does not have any terminal/console related stuff so that it can be solved separately. Introduces inline hints in debugger: <img width="1141" alt="image" src="https://github.com/user-attachments/assets/b0575f1e-ddf8-41fe-8958-2da6d4974912" /> Adds locators for go, so that you can your app in debug mode: <img width="706" alt="image" src="https://github.com/user-attachments/assets/df29bba5-8264-4bea-976f-686c32a5605b" /> As well is allows you to specify an existing compiled binary: <img width="604" alt="image" src="https://github.com/user-attachments/assets/548f2ab5-88c1-41fb-af84-115a19e685ea" /> Release Notes: - Added inline value hints for Go debugging, displaying variable values directly in the editor during debug sessions - Added Go debug locator support, enabling debugging of Go applications through task templates - Improved Go debug adapter to support both source debugging (mode: "debug") and binary execution (mode: "exec") based on program path cc @osiewicz, @Anthony-Eid
This commit is contained in:
parent
c0a5ace8b8
commit
94a5fe265d
8 changed files with 651 additions and 9 deletions
|
@ -56,5 +56,7 @@ async-pipe.workspace = true
|
|||
gpui = { workspace = true, features = ["test-support"] }
|
||||
settings = { workspace = true, features = ["test-support"] }
|
||||
task = { workspace = true, features = ["test-support"] }
|
||||
tree-sitter.workspace = true
|
||||
tree-sitter-go.workspace = true
|
||||
util = { workspace = true, features = ["test-support"] }
|
||||
zlog.workspace = true
|
||||
|
|
|
@ -275,3 +275,386 @@ impl InlineValueProvider for PythonInlineValueProvider {
|
|||
variables
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GoInlineValueProvider;
|
||||
|
||||
impl InlineValueProvider for GoInlineValueProvider {
|
||||
fn provide(
|
||||
&self,
|
||||
mut node: language::Node,
|
||||
source: &str,
|
||||
max_row: usize,
|
||||
) -> Vec<InlineValueLocation> {
|
||||
let mut variables = Vec::new();
|
||||
let mut variable_names = HashSet::new();
|
||||
let mut scope = VariableScope::Local;
|
||||
|
||||
loop {
|
||||
let mut variable_names_in_scope = HashMap::new();
|
||||
for child in node.named_children(&mut node.walk()) {
|
||||
if child.start_position().row >= max_row {
|
||||
break;
|
||||
}
|
||||
|
||||
if scope == VariableScope::Local {
|
||||
match child.kind() {
|
||||
"var_declaration" => {
|
||||
for var_spec in child.named_children(&mut child.walk()) {
|
||||
if var_spec.kind() == "var_spec" {
|
||||
if let Some(name_node) = var_spec.child_by_field_name("name") {
|
||||
let variable_name =
|
||||
source[name_node.byte_range()].to_string();
|
||||
|
||||
if variable_names.contains(&variable_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(index) =
|
||||
variable_names_in_scope.get(&variable_name)
|
||||
{
|
||||
variables.remove(*index);
|
||||
}
|
||||
|
||||
variable_names_in_scope
|
||||
.insert(variable_name.clone(), variables.len());
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Local,
|
||||
lookup: VariableLookupKind::Variable,
|
||||
row: name_node.end_position().row,
|
||||
column: name_node.end_position().column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"short_var_declaration" => {
|
||||
if let Some(left_side) = child.child_by_field_name("left") {
|
||||
for identifier in left_side.named_children(&mut left_side.walk()) {
|
||||
if identifier.kind() == "identifier" {
|
||||
let variable_name =
|
||||
source[identifier.byte_range()].to_string();
|
||||
|
||||
if variable_names.contains(&variable_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(index) =
|
||||
variable_names_in_scope.get(&variable_name)
|
||||
{
|
||||
variables.remove(*index);
|
||||
}
|
||||
|
||||
variable_names_in_scope
|
||||
.insert(variable_name.clone(), variables.len());
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Local,
|
||||
lookup: VariableLookupKind::Variable,
|
||||
row: identifier.end_position().row,
|
||||
column: identifier.end_position().column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"assignment_statement" => {
|
||||
if let Some(left_side) = child.child_by_field_name("left") {
|
||||
for identifier in left_side.named_children(&mut left_side.walk()) {
|
||||
if identifier.kind() == "identifier" {
|
||||
let variable_name =
|
||||
source[identifier.byte_range()].to_string();
|
||||
|
||||
if variable_names.contains(&variable_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(index) =
|
||||
variable_names_in_scope.get(&variable_name)
|
||||
{
|
||||
variables.remove(*index);
|
||||
}
|
||||
|
||||
variable_names_in_scope
|
||||
.insert(variable_name.clone(), variables.len());
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Local,
|
||||
lookup: VariableLookupKind::Variable,
|
||||
row: identifier.end_position().row,
|
||||
column: identifier.end_position().column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"function_declaration" | "method_declaration" => {
|
||||
if let Some(params) = child.child_by_field_name("parameters") {
|
||||
for param in params.named_children(&mut params.walk()) {
|
||||
if param.kind() == "parameter_declaration" {
|
||||
if let Some(name_node) = param.child_by_field_name("name") {
|
||||
let variable_name =
|
||||
source[name_node.byte_range()].to_string();
|
||||
|
||||
if variable_names.contains(&variable_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(index) =
|
||||
variable_names_in_scope.get(&variable_name)
|
||||
{
|
||||
variables.remove(*index);
|
||||
}
|
||||
|
||||
variable_names_in_scope
|
||||
.insert(variable_name.clone(), variables.len());
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Local,
|
||||
lookup: VariableLookupKind::Variable,
|
||||
row: name_node.end_position().row,
|
||||
column: name_node.end_position().column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"for_statement" => {
|
||||
if let Some(clause) = child.named_child(0) {
|
||||
if clause.kind() == "for_clause" {
|
||||
if let Some(init) = clause.named_child(0) {
|
||||
if init.kind() == "short_var_declaration" {
|
||||
if let Some(left_side) =
|
||||
init.child_by_field_name("left")
|
||||
{
|
||||
if left_side.kind() == "expression_list" {
|
||||
for identifier in left_side
|
||||
.named_children(&mut left_side.walk())
|
||||
{
|
||||
if identifier.kind() == "identifier" {
|
||||
let variable_name = source
|
||||
[identifier.byte_range()]
|
||||
.to_string();
|
||||
|
||||
if variable_names
|
||||
.contains(&variable_name)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(index) =
|
||||
variable_names_in_scope
|
||||
.get(&variable_name)
|
||||
{
|
||||
variables.remove(*index);
|
||||
}
|
||||
|
||||
variable_names_in_scope.insert(
|
||||
variable_name.clone(),
|
||||
variables.len(),
|
||||
);
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Local,
|
||||
lookup:
|
||||
VariableLookupKind::Variable,
|
||||
row: identifier.end_position().row,
|
||||
column: identifier
|
||||
.end_position()
|
||||
.column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if clause.kind() == "range_clause" {
|
||||
if let Some(left) = clause.child_by_field_name("left") {
|
||||
if left.kind() == "expression_list" {
|
||||
for identifier in left.named_children(&mut left.walk())
|
||||
{
|
||||
if identifier.kind() == "identifier" {
|
||||
let variable_name =
|
||||
source[identifier.byte_range()].to_string();
|
||||
|
||||
if variable_name == "_" {
|
||||
continue;
|
||||
}
|
||||
|
||||
if variable_names.contains(&variable_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(index) =
|
||||
variable_names_in_scope.get(&variable_name)
|
||||
{
|
||||
variables.remove(*index);
|
||||
}
|
||||
variable_names_in_scope.insert(
|
||||
variable_name.clone(),
|
||||
variables.len(),
|
||||
);
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Local,
|
||||
lookup: VariableLookupKind::Variable,
|
||||
row: identifier.end_position().row,
|
||||
column: identifier.end_position().column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if child.kind() == "var_declaration" {
|
||||
for var_spec in child.named_children(&mut child.walk()) {
|
||||
if var_spec.kind() == "var_spec" {
|
||||
if let Some(name_node) = var_spec.child_by_field_name("name") {
|
||||
let variable_name = source[name_node.byte_range()].to_string();
|
||||
variables.push(InlineValueLocation {
|
||||
variable_name,
|
||||
scope: VariableScope::Global,
|
||||
lookup: VariableLookupKind::Expression,
|
||||
row: name_node.end_position().row,
|
||||
column: name_node.end_position().column,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable_names.extend(variable_names_in_scope.keys().cloned());
|
||||
|
||||
if matches!(node.kind(), "function_declaration" | "method_declaration") {
|
||||
scope = VariableScope::Global;
|
||||
}
|
||||
|
||||
if let Some(parent) = node.parent() {
|
||||
node = parent;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
variables
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tree_sitter::Parser;
|
||||
|
||||
#[test]
|
||||
fn test_go_inline_value_provider() {
|
||||
let provider = GoInlineValueProvider;
|
||||
let source = r#"
|
||||
package main
|
||||
|
||||
func main() {
|
||||
items := []int{1, 2, 3, 4, 5}
|
||||
for i, v := range items {
|
||||
println(i, v)
|
||||
}
|
||||
for j := 0; j < 10; j++ {
|
||||
println(j)
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut parser = Parser::new();
|
||||
if parser
|
||||
.set_language(&tree_sitter_go::LANGUAGE.into())
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(tree) = parser.parse(source, None) else {
|
||||
return;
|
||||
};
|
||||
let root_node = tree.root_node();
|
||||
|
||||
let mut main_body = None;
|
||||
for child in root_node.named_children(&mut root_node.walk()) {
|
||||
if child.kind() == "function_declaration" {
|
||||
if let Some(name) = child.child_by_field_name("name") {
|
||||
if &source[name.byte_range()] == "main" {
|
||||
if let Some(body) = child.child_by_field_name("body") {
|
||||
main_body = Some(body);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let Some(main_body) = main_body else {
|
||||
return;
|
||||
};
|
||||
|
||||
let variables = provider.provide(main_body, source, 100);
|
||||
assert!(variables.len() >= 2);
|
||||
|
||||
let variable_names: Vec<&str> =
|
||||
variables.iter().map(|v| v.variable_name.as_str()).collect();
|
||||
assert!(variable_names.contains(&"items"));
|
||||
assert!(variable_names.contains(&"j"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_go_inline_value_provider_counter_pattern() {
|
||||
let provider = GoInlineValueProvider;
|
||||
let source = r#"
|
||||
package main
|
||||
|
||||
func main() {
|
||||
N := 10
|
||||
for i := range N {
|
||||
println(i)
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut parser = Parser::new();
|
||||
if parser
|
||||
.set_language(&tree_sitter_go::LANGUAGE.into())
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(tree) = parser.parse(source, None) else {
|
||||
return;
|
||||
};
|
||||
let root_node = tree.root_node();
|
||||
|
||||
let mut main_body = None;
|
||||
for child in root_node.named_children(&mut root_node.walk()) {
|
||||
if child.kind() == "function_declaration" {
|
||||
if let Some(name) = child.child_by_field_name("name") {
|
||||
if &source[name.byte_range()] == "main" {
|
||||
if let Some(body) = child.child_by_field_name("body") {
|
||||
main_body = Some(body);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let Some(main_body) = main_body else {
|
||||
return;
|
||||
};
|
||||
let variables = provider.provide(main_body, source, 100);
|
||||
|
||||
let variable_names: Vec<&str> =
|
||||
variables.iter().map(|v| v.variable_name.as_str()).collect();
|
||||
assert!(variable_names.contains(&"N"));
|
||||
assert!(variable_names.contains(&"i"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue