Skip to content

Latest commit

 

History

History
71 lines (56 loc) · 3.59 KB

File metadata and controls

71 lines (56 loc) · 3.59 KB

Fold Python Docstrings Design Document

Author: [Your Name]

Introduction

Rationale

The purpose of this extension is to improve the readability and manageability of Python code by automatically folding multiline docstrings ("""...""") when a Python file is opened in VS Code. Currently, VS Code does not offer a built-in feature to automatically fold docstrings, which can make navigating large codebases difficult.

Background

Python docstrings are often used to document functions, classes, and modules. However, when working with large files, these docstrings can clutter the screen, making it harder to focus on the logic. The extension aims to enhance the developer experience by automatically collapsing docstrings when opening Python files.

Terminology

  • Docstring: A string literal used for documentation purposes in Python, enclosed in triple double quotes ("""...""").
  • Folding Range: A section of the code that can be collapsed or expanded in VS Code.

Non-Goals

  • This extension does not provide syntax highlighting or linting for docstrings.
  • It does not modify the contents of the file, only its visual representation.
  • It does not fold other types of multiline strings that are not docstrings.

Proposed Design

System Architecture

The extension operates by listening to events when a Python file is opened and then programmatically folding all multiline docstrings using the VS Code API.

Data Model

  • vscode.TextEditor: Represents the active text editor.
  • vscode.TextDocument: Represents the document being edited.
  • Regex (""".*?"""): Used to identify multiline docstrings.
  • vscode.FoldingRange: Defines the ranges of lines to be folded.

Interface/API Definitions

  • The extension contributes the following command:
    "contributes": {
      "commands": [{
        "command": "fold-python-docstrings.foldDocstrings",
        "title": "Fold Python Docstrings"
      }]
    }
  • Activation is triggered on Python file load (onLanguage:python).
  • The activate function registers an event listener for text editor changes and invokes foldDocstrings when applicable.
  • The foldDocstrings function extracts docstring positions using regex and applies folding ranges.

Business Logic

  1. Detect Python File Load:
    • Use vscode.window.onDidChangeActiveTextEditor to detect when a Python file is opened.
  2. Find Docstrings:
    • Use a regex pattern to identify multiline docstrings.
  3. Apply Folding:
    • Convert detected docstrings into vscode.FoldingRange objects and fold them using editor.foldAll.

Migration Strategy

There are no breaking changes since this is a new feature. Future updates may include configuration options to allow users to customize which docstrings get folded.

Impact

  • Performance: The extension is lightweight and operates only when a Python file is opened.
  • Usability: Improves code readability by reducing visual clutter.
  • Security: No external dependencies or security risks.

Risks

  • False Positives: The regex approach may occasionally match unintended multiline strings.
  • User Preferences: Some users may prefer certain docstrings to remain unfolded.
  • Performance in Large Files: Folding all docstrings in a large file might introduce minor performance overhead.

Alternatives

  • Manual Folding: Users can manually fold docstrings, but this is inefficient for large projects.
  • Custom VS Code Settings: No built-in settings currently exist to fold docstrings automatically.
  • Configurable Extension: A future version may introduce settings to allow user customization.