message: Extract code for precise error location to a new function
diff: @@ -116,35 +116,45 @@ export class CompilerLogParser {
  return false
  }
  - showCompilerDiagnostics(compilerDiagnostics: vscode.DiagnosticCollection, buildLog: LogEntry[], source: string) {
  - compilerDiagnostics.clear()
  - const diagsCollection = Object.create(null) as { [key: string]: vscode.Diagnostic[] }
  - for (const item of buildLog) {
  - let startChar = 0
  - let endChar = 65535
  - if (item.errorPosText) {
  - // try to find the errorPosText in the respective line of the document
  - try {
  - const document = this.extension.manager.getDirtyContent(item.file) || ""
  - const lines = document.split('\n')
  + private getErrorPosition(item: LogEntry): {start: number, end: number} | undefined {
  + if (!item.errorPosText) {
  + return undefined
  + }
  + const content = this.extension.manager.getDirtyContent(item.file)
  + if (!content) {
  + return undefined
  + }
  + // Try to find the errorPosText in the respective line of the document
  + const lines = content.split('\n')
  if (lines.length >= item.line) {
  const line = lines[item.line-1]
  let pos = line.indexOf(item.errorPosText)
  if (pos >= 0) {
  pos += item.errorPosText.length
  - // find the length of the last word in the error
  + // Find the length of the last word in the error.
  // This is the length of the error-range
  const len = item.errorPosText.length - item.errorPosText.lastIndexOf(' ') - 1
  if (len > 0) {
  - startChar = pos - len
  - endChar = pos
  + return {start: pos - len, end: pos}
  }
  }
  }
  - } catch (e) {
  - // ignore error - if we cannot open the text document just continue and highlight the whole line
  + return undefined
  }
  +
  + showCompilerDiagnostics(compilerDiagnostics: vscode.DiagnosticCollection, buildLog: LogEntry[], source: string) {
  + compilerDiagnostics.clear()
  + const diagsCollection = Object.create(null) as { [key: string]: vscode.Diagnostic[] }
  + for (const item of buildLog) {
  + let startChar = 0
  + let endChar = 65535
  + // Try to compute a more precise position
  + const preciseErrorPos = this.getErrorPosition(item)
  + if (preciseErrorPos) {
  + startChar = preciseErrorPos.start
  + endChar = preciseErrorPos.end
  }
  +
  const range = new vscode.Range(new vscode.Position(item.line - 1, startChar), new vscode.Position(item.line - 1, endChar))
  const diag = new vscode.Diagnostic(range, item.text, DIAGNOSTIC_SEVERITY[item.type])
  diag.source = source
  