Skip to content

Improve Code Folding Strategy for Large String Literals and Variable Declarations #4523

Open
@luxixing

Description

@luxixing

Validations

  • I believe this is a way to improve. I'll try to join the Continue Discord for questions
  • I'm not able to find an open issue that requests the same enhancement

Problem

Issue: Improve Code Folding Strategy for Large String Literals and Variable Declarations

Problem Statement

The current code folding implementation in code.ts doesn't properly handle large string literals or array declarations, especially when they appear at the beginning of a file or function. The collapseChildren method lacks a strategy for handling these edge cases, causing entire blocks to be unnecessarily folded.

Description

The current implementation focuses on folding function bodies and class definitions, but doesn't have dedicated handling for large string literals or variable declarations. This is particularly problematic in languages like Go where large global string declarations or constant arrays are common:

// Example in Go: This large string declaration at file level causes issues
var longTemplateString = `This is a very long template string that might span several 
hundred characters and contain a lot of content...
... many more lines ...
and finally ends here.`

// Or a large array/slice declaration
var largeDataArray = []string{
    "very long string 1 that exceeds reasonable line length and causes folding issues",
    "very long string 2 that exceeds reasonable line length and causes folding issues",
    "very long string 3 that exceeds reasonable line length and causes folding issues",
    // Many more entries...
}

func main() {
    // Function body starts here
}

In such cases, the entire file gets considered for folding rather than just the problematic declaration.

Suggested Improvement

Enhance the collapseChildren method to include a line-based approach for handling very long lines:

  1. Add a character threshold for individual lines (e.g., 500 characters)
  2. When processing code content, check if any individual line exceeds this threshold
  3. For lines that exceed the threshold, apply targeted folding just to those lines

Implementation Approach

// Add a function to handle long lines in addition to existing folding strategies
function handleLongLines(code: string, maxLineLength: number = 500): string {
  const lines = code.split('\n');
  
  return lines.map(line => {
    if (line.length > maxLineLength) {
      // Preserve the first 200 and last 100 characters
      return line.substring(0, 200) + '...' + line.substring(line.length - 100);
    }
    return line;
  }).join('\n');
}

// Update collapseChildren to use this function
async function collapseChildren(
  node: SyntaxNode,
  code: string,
  blockTypes: string[],
  collapseTypes: string[],
  collapseBlockTypes: string[],
  maxChunkSize: number,
): Promise<string> {
  // Existing code...
  
  // Add line length handling before other folding logic
  code = handleLongLines(code);
  
  // Rest of the existing implementation...
}

This enhancement would allow for more granular folding, targeting only the problematic long lines rather than folding entire blocks or files.

Benefits

  1. More precise folding for large literals and declarations
  2. Improved readability of indexed code
  3. Better handling of edge cases in languages like Go, Java, and Python
  4. Reduced overall size of indexed content without losing important context

Related Components

  • code.ts - Main file for implementation
  • Tree-sitter parsing integration
  • Languages with common long string patterns (Go, Java, Python, etc.)

Solution

No response

Metadata

Metadata

Assignees

Labels

kind:enhancementIndicates a new feature request, imrovement, or extensionneeds-triage

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions