Skip to content

Commit

Permalink
change line error display code to better understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
rush93 committed Sep 1, 2022
1 parent 8743761 commit 427c83a
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,28 +362,11 @@ async function refreshDiagnostics(result: ResultType) {
for (let path in result.files) {
const pathItem = result.files[path]
const diagnostics: vscode.Diagnostic[] = []
let document: vscode.TextDocument = null
try {
document = await vscode.workspace.openTextDocument(
vscode.Uri.file(path)
)
} catch (error) {
setStatusBarError(error, "Document not found")
}
const document = await getDocumentFromPath(path)

for (const messageItem of pathItem.messages) {
const line = messageItem.line ? messageItem.line - 1 : 0
let range: vscode.Range = null
if (document) {
const lineText = document?.lineAt(line)
range = new vscode.Range(
line,
lineText.firstNonWhitespaceCharacterIndex,
line,
lineText.range.end.character
)
} else {
range = new vscode.Range(line, 0, line, 0)
}
const range = getRangeFromDocumentAndLine(document, line)
const diagnostic = new vscode.Diagnostic(
range,
messageItem.message,
Expand All @@ -401,6 +384,31 @@ async function refreshDiagnostics(result: ResultType) {
}
}

async function getDocumentFromPath(path: string) {
try {
return await vscode.workspace.openTextDocument(vscode.Uri.file(path))
} catch (error) {
setStatusBarError(error, "Document not found")
return null
}
}

function getRangeFromDocumentAndLine(
document: vscode.TextDocument | null,
line: number
) {
if (!document) {
return new vscode.Range(line, 0, line, 0)
}
const textLine = document?.lineAt(line)
return new vscode.Range(
line,
textLine.firstNonWhitespaceCharacterIndex,
line,
textLine.range.end.character
)
}

function getWorkspacePath() {
const [folder] = vscode.workspace.workspaceFolders || []
return folder ? folder.uri.fsPath : null
Expand Down

0 comments on commit 427c83a

Please sign in to comment.