settings.rs

  1use std::ops::Range;
  2use tree_sitter::{Query, QueryMatch};
  3
  4use crate::MigrationPatterns;
  5
  6pub const SETTINGS_PATTERNS: MigrationPatterns = &[(
  7    SETTINGS_CONTEXT_SERVER_PATTERN,
  8    flatten_context_server_command,
  9)];
 10
 11const SETTINGS_CONTEXT_SERVER_PATTERN: &str = r#"(document
 12    (object
 13        (pair
 14            key: (string (string_content) @context-servers)
 15            value: (object
 16                (pair
 17                    key: (string (string_content) @server-name)
 18                    value: (object
 19                        (pair
 20                            key: (string (string_content) @source-key)
 21                            value: (string (string_content) @source-value)
 22                        )
 23                        (pair
 24                            key: (string (string_content) @command-key)
 25                            value: (object) @command-object
 26                        ) @command-pair
 27                    ) @server-settings
 28                )
 29            )
 30        )
 31    )
 32    (#eq? @context-servers "context_servers")
 33    (#eq? @source-key "source")
 34    (#eq? @source-value "custom")
 35    (#eq? @command-key "command")
 36)"#;
 37
 38fn flatten_context_server_command(
 39    contents: &str,
 40    mat: &QueryMatch,
 41    query: &Query,
 42) -> Option<(Range<usize>, String)> {
 43    let command_pair_index = query.capture_index_for_name("command-pair")?;
 44    let command_pair = mat.nodes_for_capture_index(command_pair_index).next()?;
 45
 46    let command_object_index = query.capture_index_for_name("command-object")?;
 47    let command_object = mat.nodes_for_capture_index(command_object_index).next()?;
 48
 49    let server_settings_index = query.capture_index_for_name("server-settings")?;
 50    let _server_settings = mat.nodes_for_capture_index(server_settings_index).next()?;
 51
 52    // Parse the command object to extract path, args, and env
 53    let mut path_value = None;
 54    let mut args_value = None;
 55    let mut env_value = None;
 56
 57    let mut cursor = command_object.walk();
 58    for child in command_object.children(&mut cursor) {
 59        if child.kind() == "pair"
 60            && let Some(key_node) = child.child_by_field_name("key")
 61                && let Some(string_content) = key_node.child(1) {
 62                    let key = &contents[string_content.byte_range()];
 63                    if let Some(value_node) = child.child_by_field_name("value") {
 64                        let value_range = value_node.byte_range();
 65                        match key {
 66                            "path" => path_value = Some(&contents[value_range]),
 67                            "args" => args_value = Some(&contents[value_range]),
 68                            "env" => env_value = Some(&contents[value_range]),
 69                            _ => {}
 70                        }
 71                    }
 72                }
 73    }
 74
 75    let path = path_value?;
 76
 77    // Get the proper indentation from the command pair
 78    let command_pair_start = command_pair.start_byte();
 79    let line_start = contents[..command_pair_start]
 80        .rfind('\n')
 81        .map(|pos| pos + 1)
 82        .unwrap_or(0);
 83    let indent = &contents[line_start..command_pair_start];
 84
 85    // Build the replacement string
 86    let mut replacement = format!("\"command\": {}", path);
 87
 88    // Add args if present - need to reduce indentation
 89    if let Some(args) = args_value {
 90        replacement.push_str(",\n");
 91        replacement.push_str(indent);
 92        replacement.push_str("\"args\": ");
 93        let reduced_args = reduce_indentation(args, 4);
 94        replacement.push_str(&reduced_args);
 95    }
 96
 97    // Add env if present - need to reduce indentation
 98    if let Some(env) = env_value {
 99        replacement.push_str(",\n");
100        replacement.push_str(indent);
101        replacement.push_str("\"env\": ");
102        replacement.push_str(&reduce_indentation(env, 4));
103    }
104
105    let range_to_replace = command_pair.byte_range();
106    Some((range_to_replace, replacement))
107}
108
109fn reduce_indentation(text: &str, spaces: usize) -> String {
110    let lines: Vec<&str> = text.lines().collect();
111    let mut result = String::new();
112
113    for (i, line) in lines.iter().enumerate() {
114        if i > 0 {
115            result.push('\n');
116        }
117
118        // Count leading spaces
119        let leading_spaces = line.chars().take_while(|&c| c == ' ').count();
120
121        if leading_spaces >= spaces {
122            // Reduce indentation
123            result.push_str(&line[spaces..]);
124        } else {
125            // Keep line as is if it doesn't have enough indentation
126            result.push_str(line);
127        }
128    }
129
130    result
131}