Skip to main content
The Editor API provides interfaces for accessing and manipulating text editors in Android Code Studio.

Core Interfaces

IEditor

The main interface for accessing editor instances and manipulating their content. Source: editor/api/src/main/java/com/tom/rv2ide/editor/api/IEditor.java:34
package com.tom.rv2ide.editor.api;

import com.tom.rv2ide.models.Position;
import com.tom.rv2ide.models.Range;
import java.io.File;

public interface IEditor {
  String KEY_FILE = "ide.editor.file";
  
  // File operations
  File getFile();
  boolean isModified();
  
  // Selection operations
  void setSelection(Position position);
  void setSelection(Range range);
  void setSelection(Position start, Position end);
  void setSelectionAround(int line, int column);
  
  // Cursor position
  Range getCursorLSPRange();
  Position getCursorLSPPosition();
  
  // Validation
  void validateRange(Range range);
  boolean isValidRange(Range range);
  boolean isValidRange(Range range, boolean allowColumnEqual);
  boolean isValidPosition(Position position);
  boolean isValidPosition(Position position, boolean allowColumnEqual);
  boolean isValidLine(int line);
  boolean isValidColumn(int line, int column);
  boolean isValidColumn(int line, int column, boolean allowColumnEqual);
  
  // Content manipulation
  int append(CharSequence text);
  void replaceContent(CharSequence newContent);
  void goToEnd();
}

File Operations

Get Current File

Retrieve the file being edited:
File file = editor.getFile();
if (file != null) {
  String path = file.getAbsolutePath();
  System.out.println("Editing: " + path);
}
getFile()
File
Returns the file being edited, or null if no file is associated

Check Modification Status

Determine if the editor content differs from the file on disk:
if (editor.isModified()) {
  // Save prompt or auto-save logic
  System.out.println("File has unsaved changes");
}
isModified()
boolean
Returns true if content has been modified, false otherwise

Selection Management

Set Selection by Position

Move the cursor to a specific position:
import com.tom.rv2ide.models.Position;

// Move to line 10, column 5 (0-based)
Position pos = new Position(10, 5);
editor.setSelection(pos);
position
Position
required
The position to move the cursor to. Line and column are 0-based.

Set Selection by Range

Select a range of text:
import com.tom.rv2ide.models.Range;

// Select from (10,5) to (10,15)
Position start = new Position(10, 5);
Position end = new Position(10, 15);
Range range = new Range(start, end);
editor.setSelection(range);

// Or use the two-parameter version
editor.setSelection(start, end);
range
Range
required
The range to select. Includes start and end positions.

Select Around Position

Set selection around a specific line and column:
// Select word at line 15, column 10
editor.setSelectionAround(15, 10);

Cursor Information

Get Cursor Position

Retrieve the current cursor position:
Position cursor = editor.getCursorLSPPosition();
int line = cursor.getLine();
int column = cursor.getColumn();
System.out.println("Cursor at: " + line + "," + column);
getCursorLSPPosition()
Position
Returns the current cursor position in LSP format (0-based line and column)

Get Selection Range

Retrieve the selected range:
Range selection = editor.getCursorLSPRange();
Position start = selection.getStart();
Position end = selection.getEnd();

if (start.equals(end)) {
  System.out.println("No selection (cursor only)");
} else {
  System.out.println("Selected from " + start + " to " + end);
}
getCursorLSPRange()
Range
Returns the selection range, or a range with identical start and end if no selection

Content Validation

Validate Ranges

Check if a range is valid for the current content:
Range range = new Range(new Position(100, 0), new Position(101, 0));

if (editor.isValidRange(range)) {
  editor.setSelection(range);
} else {
  System.err.println("Invalid range for current content");
}
allowColumnEqual
boolean
default:"false"
Whether to allow column values equal to the line’s column count (useful for end-of-line positions)

Validate Positions

Position pos = new Position(50, 20);

if (!editor.isValidPosition(pos)) {
  System.err.println("Position out of bounds");
  return;
}

editor.setSelection(pos);

Validate Lines and Columns

int line = 100;
if (editor.isValidLine(line)) {
  int column = 50;
  if (editor.isValidColumn(line, column)) {
    editor.setSelectionAround(line, column);
  }
}

Content Manipulation

Append Text

Add text at the end of the editor:
String newCode = "\n// TODO: Implement this\n";
int lineAdded = editor.append(newCode);
System.out.println("Text appended at line: " + lineAdded);
text
CharSequence
required
The text to append to the editor
append()
int
Returns the line number where the text was appended

Replace Content

Replace the entire editor content:
String newContent = "package com.example;\n\npublic class Main {}";
editor.replaceContent(newContent);
This replaces all content in the editor. Use with caution.
Move cursor to the last line:
editor.goToEnd();
// Cursor is now at the end of the file

LSP Integration

ILspEditor

Extends editor functionality with Language Server Protocol features. Source: editor/api/src/main/java/com/tom/rv2ide/editor/api/ILspEditor.kt:30
package com.tom.rv2ide.editor.api

import com.tom.rv2ide.lsp.api.ILanguageClient
import com.tom.rv2ide.lsp.api.ILanguageServer
import com.tom.rv2ide.lsp.models.Command
import com.tom.rv2ide.lsp.models.SignatureHelp

interface ILspEditor {
  // Server connection
  fun setLanguageServer(server: ILanguageServer?)
  fun setLanguageClient(client: ILanguageClient?)
  
  // LSP commands
  fun executeCommand(command: Command?)
  
  // Language features
  fun signatureHelp()
  fun showSignatureHelp(help: SignatureHelp?)
  fun findDefinition()
  fun findReferences()
  fun expandSelection()
  
  // Window management
  fun ensureWindowsDismissed()
}

Language Features

Connect Language Server

val languageServer: ILanguageServer = getJavaLanguageServer()
lspEditor.setLanguageServer(languageServer)

Execute LSP Command

import com.tom.rv2ide.lsp.models.Command

val command = Command(
  title = "Organize Imports",
  command = "java.edit.organizeImports",
  arguments = listOf(fileUri)
)

lspEditor.executeCommand(command)

Trigger Signature Help

Show parameter hints at the cursor:
// Automatically requests and shows signature help
lspEditor.signatureHelp()

Find Definition

Navigate to symbol definition:
// Finds and navigates to definition at cursor
lspEditor.findDefinition()

Find References

Find all references to the symbol at cursor:
// Finds all references and shows in search results
lspEditor.findReferences()

Expand Selection

Expand selection semantically:
// Expands selection to next larger semantic unit
lspEditor.expandSelection()

Complete Example

Here’s a complete example showing various editor operations:
import com.tom.rv2ide.editor.api.IEditor;
import com.tom.rv2ide.editor.api.ILspEditor;
import com.tom.rv2ide.models.Position;
import com.tom.rv2ide.models.Range;

public class EditorExample {
  
  public void manipulateEditor(IEditor editor, ILspEditor lspEditor) {
    // Check if file is modified
    if (editor.isModified()) {
      System.out.println("File: " + editor.getFile().getName() + " has changes");
    }
    
    // Get current cursor position
    Position cursor = editor.getCursorLSPPosition();
    System.out.println("Cursor at: " + cursor.getLine() + "," + cursor.getColumn());
    
    // Select a range
    Position start = new Position(10, 0);
    Position end = new Position(15, 0);
    
    if (editor.isValidPosition(start) && editor.isValidPosition(end)) {
      editor.setSelection(start, end);
      System.out.println("Selected lines 10-15");
    }
    
    // Append new content
    int line = editor.append("\n// New comment\n");
    System.out.println("Added comment at line: " + line);
    
    // Navigate to end
    editor.goToEnd();
    
    // Use LSP features
    lspEditor.findDefinition();
  }
}

LSP API

Implement language server features

Action System

Add editor actions and commands

Build docs developers (and LLMs) love