import { TextEditor , Position , Selection } from 'vscode' ; import { StringUtil } from '../Utility/StringUtil' ; export class VSCodeApi { private _activeEditor : TextEditor ; constructor ( activeEditor ) { this . _activeEditor = activeEditor ; } public IsLanguage ( languageId ) { return ( this . _activeEditor . document . languageId === languageId ) ; } public GetActivePosition ( ) { return this . _activeEditor . selection . active ; } public GetActiveLine ( ) { return this . GetActivePosition ( ) . line ; } public GetLineCount ( ) { return this . _activeEditor . document . lineCount ; } public GetActiveCharPosition ( ) { return this . _activeEditor . selection . active . character ; } public GetPosition ( line , charcter ) { return new Position ( line , charcter ) ; } public ShiftPositionLine ( position , offset ) { return this . GetPosition ( position . line + offset , position . character ) ; } public ShiftPositionChar ( position , offset ) { return this . GetPosition ( position . line , position . character + offset ) ; } public GetSelection ( line , charcter ) { return new Selection ( line , charcter , line , charcter ) ; } public GetSelectionByPosition ( anchor , active ) { return new Selection ( anchor , active ) ; } public MoveSelection ( line , charcter ) { const move = this . GetSelection ( line , charcter ) ; this . _activeEditor . selection = move ; } public InsertText ( position , text ) { this . _activeEditor . edit ( ( editBuilder ) => { editBuilder . insert ( position , text ) ; } ) ; } public ReplaceText ( selection , text ) { this . _activeEditor . edit ( ( editBuilder ) => { editBuilder . replace ( selection , text ) ; } ) ; } public ReadLine ( line ) { return this . _activeEditor . document . lineAt ( line ) . text ; } public ReadLineAtCurrent ( ) { return this . ReadLine ( this . GetActiveLine ( ) ) ; } public ReadCharAtCurrent ( ) { return this . ReadLineAtCurrent ( ) . charAt ( this . GetActiveCharPosition ( ) ) ; } public ReadNextCodeFromCurrent ( eol = '\n' ) { const lineCount = this . GetLineCount ( ) ; const curLine = this . GetActiveLine ( ) ; let code = '' ; for ( let i = curLine ; i < lineCount - 1 ; i ++ ) { const line = this . ReadLine ( i + 1 ) ; if ( StringUtil . IsNullOrWhiteSpace ( line ) ) continue ; code += line + eol ; if ( ! StringUtil . IsCodeBlockStart ( line ) ) { continue ; } return StringUtil . RemoveComment ( code ) ; } return null ; } public ReadPreviousCodeFromCurrent ( ) { const curLine = this . GetActiveLine ( ) ; let code = '' ; for ( let i = curLine ; 0 < i ; i -- ) { const line = this . ReadLine ( i - 1 ) ; if ( StringUtil . IsNullOrWhiteSpace ( line ) ) continue ; code += line ; if ( ! StringUtil . IsCodeBlockStart ( line ) ) { continue ; } return code ; } return null ; } public ReadPreviousLineFromCurrent ( ) { const curLine = this . GetActiveLine ( ) ; for ( let i = curLine ; 0 < i ; i -- ) { const line = this . ReadLine ( i - 1 ) ; if ( StringUtil . IsNullOrWhiteSpace ( line ) ) continue ; return line ; } return null ; } public ReadNextLineFromCurrent ( ) { const lineCount = this . GetLineCount ( ) ; const curLine = this . GetActiveLine ( ) ; for ( let i = curLine ; i < lineCount - 1 ; i ++ ) { const line = this . ReadLine ( i + 1 ) ; if ( StringUtil . IsNullOrWhiteSpace ( line ) ) continue ; return line ; } return null ; } }