Skip to content

Get Active Editor

The getActiveEditor action retrieves information about the currently active editor in the host IDE, including file paths, language, cursor/selection position, and optionally the full file content.

import { getActiveEditor } from '@unextension/bridge'

Returns information about the currently active editor, or null if no editor is open.

const editor = await getActiveEditor()
if (editor) {
console.log(editor.relativePath) // 'src/index.ts'
console.log(editor.language) // 'typescript'
console.log(editor.startLine, editor.startColumn) // cursor position
}
ParameterTypeDescription
optionsGetActiveEditorOptionsOptional configuration
OptionTypeDefaultDescription
includeContentbooleanfalseWhen true, includes the full file text in the result

Promise<GetActiveEditorResult | null>

Returns null when no editor is currently open in the IDE.


FieldTypeRequiredDescription
relativePathstringYesFile path relative to the project root
absolutePathstringYesFull filesystem path of the active file
languagestringYesLanguage identifier as reported by the IDE (e.g. typescript)
startLinenumberYesStart line of cursor or selection (0-indexed)
startColumnnumberYesStart column of cursor or selection (0-indexed)
endLinenumberYesEnd line of cursor or selection (0-indexed)
endColumnnumberYesEnd column of cursor or selection (0-indexed)
selectionstringNoSelected text (only present when text is selected)
contentstringNoFull file content (only present when includeContent is true)

const editor = await getActiveEditor()
if (editor) {
console.log(`File: ${editor.relativePath}`)
console.log(`Language: ${editor.language}`)
console.log(`Cursor: line ${editor.startLine}, column ${editor.startColumn}`)
} else {
console.log('No editor is open')
}
const editor = await getActiveEditor({ includeContent: true })
if (editor) {
console.log(`File: ${editor.relativePath}`)
console.log(`Content length: ${editor.content?.length} chars`)
}

When the user has text selected, startLine/startColumn and endLine/endColumn define the selection range, and the selection field contains the selected text:

const editor = await getActiveEditor()
if (editor?.selection) {
console.log(`Selected: "${editor.selection}"`)
console.log(
`From ${editor.startLine}:${editor.startColumn} to ${editor.endLine}:${editor.endColumn}`,
)
}

When no text is selected, startLine === endLine and startColumn === endColumn, representing the cursor position. The selection field is not present.


getActiveEditor returns null when no editor is currently open in the IDE. Always check for null before accessing result fields:

const editor = await getActiveEditor()
if (!editor) {
// No editor is open — handle the empty state
return
}
// Safe to use editor fields here
console.log(editor.relativePath)

All types are exported from @unextension/bridge:

import type { GetActiveEditorOptions, GetActiveEditorResult } from '@unextension/bridge'
interface GetActiveEditorOptions {
includeContent?: boolean
}
interface GetActiveEditorResult {
relativePath: string
absolutePath: string
language: string
startLine: number
startColumn: number
endLine: number
endColumn: number
selection?: string
content?: string
}