Editor¶
Text editor operations including selections, decorations, and snippets.
Editor
¶
VS Code editor operations.
Source code in vscode_sockpuppet/editor.py
get_selection
¶
Get the current selection in the active editor.
Returns:
Type | Description |
---|---|
dict
|
Selection information with start, end positions and text |
set_selection
¶
Set the selection in the active editor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_line
|
int
|
Starting line number |
required |
start_char
|
int
|
Starting character position |
required |
end_line
|
int
|
Ending line number |
required |
end_char
|
int
|
Ending character position |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Source code in vscode_sockpuppet/editor.py
insert_text
¶
Insert text at a position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
int
|
Line number |
required |
character
|
int
|
Character position |
required |
text
|
str
|
Text to insert |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Source code in vscode_sockpuppet/editor.py
delete_range
¶
Delete text in a range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_line
|
int
|
Starting line number |
required |
start_char
|
int
|
Starting character position |
required |
end_line
|
int
|
Ending line number |
required |
end_char
|
int
|
Ending character position |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Source code in vscode_sockpuppet/editor.py
replace_text
¶
Replace text in a range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_line
|
int
|
Starting line number |
required |
start_char
|
int
|
Starting character position |
required |
end_line
|
int
|
Ending line number |
required |
end_char
|
int
|
Ending character position |
required |
text
|
str
|
Replacement text |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Source code in vscode_sockpuppet/editor.py
edit
¶
edit(callback: Callable[[EditBuilder], None], undo_stop_before: bool = True, undo_stop_after: bool = True) -> bool
Perform complex edits with multiple operations atomically.
This is the preferred way to make multiple edits as they will be applied together in a single transaction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback
|
Callable[[EditBuilder], None]
|
Function that receives an EditBuilder and adds edits |
required |
undo_stop_before
|
bool
|
Add undo stop before edits |
True
|
undo_stop_after
|
bool
|
Add undo stop after edits |
True
|
Returns:
Type | Description |
---|---|
bool
|
True if edits were applied successfully |
Example
def make_edits(edit_builder): edit_builder.insert(0, 0, "# Header\n") edit_builder.replace(5, 0, 5, 10, "new text") edit_builder.delete(10, 0, 11, 0)
editor.edit(make_edits)
Source code in vscode_sockpuppet/editor.py
insert_snippet
¶
insert_snippet(snippet: str, location: Optional[Union[PositionType, RangeType]] = None, undo_stop_before: bool = True, undo_stop_after: bool = True) -> bool
Insert a snippet at the current selection or specified location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
snippet
|
str
|
Snippet string with placeholders ($1, $2, etc.) |
required |
location
|
Optional[Union[PositionType, RangeType]]
|
Position/Range (dict or dataclass), or None for current |
None
|
undo_stop_before
|
bool
|
Add undo stop before insertion |
True
|
undo_stop_after
|
bool
|
Add undo stop after insertion |
True
|
Returns:
Type | Description |
---|---|
bool
|
True if snippet was inserted successfully |
Example
Insert at current position¶
editor.insert_snippet("console.log('$1');$0")
Insert at specific position (dict)¶
editor.insert_snippet( "for (let ${1:i} = 0; $1 < ${2:10}; $1++) {\n\t$0\n}", location={"line": 5, "character": 0} )
Or use Position/Range from document.py¶
from vscode_sockpuppet.document import Position editor.insert_snippet("$0", location=Position(5, 0))
Source code in vscode_sockpuppet/editor.py
reveal_range
¶
reveal_range(start_line: int, start_char: int, end_line: int, end_char: int, reveal_type: Optional[str] = None) -> dict
Scroll to make a range visible in the editor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_line
|
int
|
Starting line number |
required |
start_char
|
int
|
Starting character position |
required |
end_line
|
int
|
Ending line number |
required |
end_char
|
int
|
Ending character position |
required |
reveal_type
|
Optional[str]
|
How to reveal: - None/Default: Scroll minimally to reveal range - 'InCenter': Scroll to center range in viewport - 'InCenterIfOutsideViewport': Center only if outside - 'AtTop': Scroll to show range at top |
None
|
Returns:
Type | Description |
---|---|
dict
|
Success status |
Example
Scroll to line 100¶
editor.reveal_range(100, 0, 100, 0)
Scroll and center line 50¶
editor.reveal_range(50, 0, 50, 0, 'InCenter')
Source code in vscode_sockpuppet/editor.py
get_selections
¶
Get all selections in the active editor.
Returns:
Type | Description |
---|---|
list[dict]
|
List of selection objects with start, end, and text |
Example
selections = editor.get_selections() for sel in selections: print(f"Selected: {sel['text']}")
Source code in vscode_sockpuppet/editor.py
set_selections
¶
Set multiple selections (multi-cursor support).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selections
|
list[RangeType]
|
List of Range objects (dict or dataclass) |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Example
Using dicts¶
editor.set_selections([ { "start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 5} }, { "start": {"line": 2, "character": 0}, "end": {"line": 2, "character": 5} } ])
Or using Range from document.py¶
from vscode_sockpuppet.document import Range, Position editor.set_selections([ Range(Position(0, 0), Position(0, 5)), Range(Position(2, 0), Position(2, 5)) ])
Source code in vscode_sockpuppet/editor.py
get_options
¶
Get editor options (tab size, insert spaces, etc.).
Returns:
Type | Description |
---|---|
dict
|
Dict with editor options |
Example
options = editor.get_options() print(f"Tab size: {options['tabSize']}") print(f"Insert spaces: {options['insertSpaces']}")
Source code in vscode_sockpuppet/editor.py
set_options
¶
Set editor options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options
|
dict
|
Dict with options to set (tabSize, insertSpaces, etc.) |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Example
editor.set_options({ "tabSize": 2, "insertSpaces": True })
Source code in vscode_sockpuppet/editor.py
get_visible_ranges
¶
Get the ranges that are currently visible in the editor.
Returns:
Type | Description |
---|---|
list[dict]
|
List of visible range objects |
Example
ranges = editor.get_visible_ranges() for r in ranges: print(f"Visible: lines {r['start']['line']}-" f"{r['end']['line']}")
Source code in vscode_sockpuppet/editor.py
get_view_column
¶
Get the view column of the active editor.
Returns:
Type | Description |
---|---|
Optional[int]
|
View column number (1-9), or None |
Example
column = editor.get_view_column() print(f"Editor in column: {column}")
Source code in vscode_sockpuppet/editor.py
create_decoration_type
¶
Create a text editor decoration type.
The returned DecorationType object will automatically dispose the server-side resource when garbage collected. Call dispose() manually for immediate cleanup.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options
|
DecorationRenderOptions
|
DecorationRenderOptions dict with styling options |
required |
Returns:
Type | Description |
---|---|
DecorationType
|
DecorationType object with |
Example
from vscode_sockpuppet import DecorationRenderOptions
Type-safe decoration options¶
options: DecorationRenderOptions = { "backgroundColor": "rgba(255, 0, 0, 0.3)", "border": "1px solid red", "borderRadius": "3px", "isWholeLine": True, "overviewRulerLane": 2, # Center "light": { "backgroundColor": "rgba(255, 0, 0, 0.1)" }, "dark": { "backgroundColor": "rgba(255, 0, 0, 0.3)" } } decoration = editor.create_decoration_type(options) editor.set_decorations(decoration, [range1, range2])
Manual disposal¶
decoration.dispose()
Source code in vscode_sockpuppet/editor.py
set_decorations
¶
Apply decorations to the active text editor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decoration
|
Union[DecorationType, str]
|
DecorationType object or decoration ID string |
required |
ranges
|
list[RangeType]
|
List of Range objects (dict or dataclass) |
required |
Returns:
Type | Description |
---|---|
dict
|
Success status |
Example
decoration = editor.create_decoration_type({ "backgroundColor": "yellow" })
Using dicts¶
ranges = [{ "start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 10} }] editor.set_decorations(decoration, ranges)
Or using Range from document.py¶
from vscode_sockpuppet.document import Range, Position ranges = [Range(Position(0, 0), Position(0, 10))] editor.set_decorations(decoration, ranges)
Source code in vscode_sockpuppet/editor.py
EditBuilder¶
EditBuilder
¶
Builder for creating multiple text edits.
Used with Editor.edit() to batch multiple edits together.
insert
¶
Add an insert operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
int
|
Line number |
required |
character
|
int
|
Character position |
required |
text
|
str
|
Text to insert |
required |
Returns:
Type | Description |
---|---|
EditBuilder
|
Self for chaining |
Source code in vscode_sockpuppet/editor.py
delete
¶
Add a delete operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_line
|
int
|
Starting line number |
required |
start_char
|
int
|
Starting character position |
required |
end_line
|
int
|
Ending line number |
required |
end_char
|
int
|
Ending character position |
required |
Returns:
Type | Description |
---|---|
EditBuilder
|
Self for chaining |
Source code in vscode_sockpuppet/editor.py
replace
¶
Add a replace operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_line
|
int
|
Starting line number |
required |
start_char
|
int
|
Starting character position |
required |
end_line
|
int
|
Ending line number |
required |
end_char
|
int
|
Ending character position |
required |
text
|
str
|
Replacement text |
required |
Returns:
Type | Description |
---|---|
EditBuilder
|
Self for chaining |
Source code in vscode_sockpuppet/editor.py
DecorationType¶
DecorationType
¶
Wrapper for a text editor decoration type.
This object automatically disposes the server-side decoration resource when it's garbage collected, preventing resource leaks.