import * as vscode from 'vscode' ; import { Diagram , diagramStartReg , diagramEndReg } from './diagram' ; import { languageid } from '../common' ; export function currentDiagram ( ) { let editor = vscode . window . activeTextEditor ; if ( editor ) return diagramAt ( editor . document , editor . selection . anchor . line ) ; } export function diagramAt ( document , lineNumber ) ; export function diagramAt ( document , position ) ; export function diagramAt ( document , para ) { let lineNumber = para instanceof vscode . Position ? para . line : para ; let start ; let end ; let content = "" ; for ( let i = lineNumber ; i >= 0 ; i -- ) { let line = document . lineAt ( i ) ; if ( diagramStartReg . test ( line . text ) ) { start = line . range . start ; break ; } else if ( i != lineNumber && diagramEndReg . test ( line . text ) ) { return undefined ; } } for ( let i = lineNumber ; i < document . lineCount ; i ++ ) { let line = document . lineAt ( i ) ; if ( diagramEndReg . test ( line . text ) ) { end = line . range . end ; break ; } else if ( i != lineNumber && diagramStartReg . test ( line . text ) ) { return undefined ; } } if ( ! ( start && end ) && document . getText ( ) . trim ( ) && document . languageId == languageid ) { start = document . lineAt ( 0 ) . range . start ; end = document . lineAt ( document . lineCount - 1 ) . range . end ; } let diagram = undefined ; if ( start && end ) { content = document . getText ( new vscode . Range ( start , end ) ) ; diagram = new Diagram ( content , document , start , end ) ; } return diagram ; } export function diagramsOf ( document ) { let diagrams = [ ] ; for ( let i = 0 ; i < document . lineCount ; i ++ ) { let line = document . lineAt ( i ) ; if ( diagramStartReg . test ( line . text ) ) { let d = diagramAt ( document , i ) ; if ( d ) diagrams . push ( d ) ; } } if ( ! diagrams . length ) { let d = diagramAt ( document , 0 ) ; if ( d ) diagrams . push ( d ) ; } return diagrams ; }