|
1 | 1 | import * as vscode from 'vscode'; |
| 2 | +import { URL } from 'url'; |
2 | 3 | import { auditData } from './storage'; |
3 | 4 | import { listFilterNotes } from './filter'; |
| 5 | +import { noteSeparator } from './importnotes'; |
4 | 6 |
|
5 | 7 |
|
6 | 8 | export function generateReport(out: vscode.OutputChannel) { |
7 | 9 | if (!auditData) { |
8 | 10 | vscode.window.showErrorMessage("Extension not ready"); |
9 | 11 | return; |
10 | 12 | } |
11 | | - for (const [fileName, fileInfo] of Object.entries(listFilterNotes())) { |
12 | | - if (Object.keys(fileInfo.notes).length === 0) { |
13 | | - continue; |
14 | | - } |
| 13 | + |
| 14 | + const filteredNotes = listFilterNotes(); |
| 15 | + for (const fileName of Object.keys(filteredNotes).sort()) { |
15 | 16 | out.appendLine(`${fileName}`); |
16 | | - for (const [lineNum, note] of Object.entries(fileInfo.notes)) { |
| 17 | + for (const [lineNum, note] of Object.entries(filteredNotes[fileName].notes)) { |
17 | 18 | let afectedLines = note.length > 1 ? `${lineNum}:${parseInt(lineNum) + note.length - 1}` : lineNum; |
18 | 19 | if (afectedLines.length < 4) { afectedLines += '\t'; } |
19 | 20 | out.appendLine(`\t${afectedLines}\t- ${note.type}: ${note.state}\t=> ${note.message}`); |
20 | 21 | } |
21 | 22 | } |
22 | 23 | } |
| 24 | + |
| 25 | +export async function generateReferences(out: vscode.OutputChannel) { |
| 26 | + if (!auditData) { |
| 27 | + vscode.window.showErrorMessage("Extension not ready"); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const inputSort = await vscode.window.showInputBox({ |
| 32 | + value: "3", |
| 33 | + prompt: "Sort by the first # words", |
| 34 | + placeHolder: "number between 1 and 10", |
| 35 | + ignoreFocusOut: true, |
| 36 | + }); |
| 37 | + if (!inputSort) { |
| 38 | + vscode.window.showErrorMessage("Operation cancelled"); |
| 39 | + return; |
| 40 | + } |
| 41 | + let sortBy = parseInt(inputSort); |
| 42 | + if (!sortBy || sortBy < 1 || sortBy > 10) { sortBy = 2; } |
| 43 | + |
| 44 | + let inputUrl = await vscode.window.showInputBox({ |
| 45 | + prompt: "Enter the base URL or leave empty", |
| 46 | + placeHolder: "optional base URL", |
| 47 | + ignoreFocusOut: true |
| 48 | + }); |
| 49 | + let baseURL; |
| 50 | + if (inputUrl) { |
| 51 | + inputUrl = inputUrl.trim(); |
| 52 | + if (!inputUrl.endsWith('/')) { |
| 53 | + inputUrl += '/'; |
| 54 | + } |
| 55 | + try { |
| 56 | + const tryURL = new URL(inputUrl); |
| 57 | + baseURL = inputUrl; |
| 58 | + } catch (error) { |
| 59 | + vscode.window.showInformationMessage("Invalid base URL"); |
| 60 | + baseURL = ""; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + const noterefs: { [key: string]: string[]; } = {}; |
| 65 | + const filteredNotes = listFilterNotes(); |
| 66 | + for (const fileName of Object.keys(filteredNotes).sort()) { |
| 67 | + for (const [lineNum, note] of Object.entries(filteredNotes[fileName].notes)) { |
| 68 | + if (!note.message) { continue; } |
| 69 | + const lineInd = baseURL ? 'L' : ''; |
| 70 | + const afectedLines = note.length > 1 ? `${lineInd}${lineNum}-${lineInd}${parseInt(lineNum) + note.length - 1}` : lineInd + lineNum; |
| 71 | + |
| 72 | + for (const x of note.message.split(noteSeparator)) { |
| 73 | + const key = x.toLowerCase().split(' ').slice(0, sortBy).join(' '); |
| 74 | + const ref = `${fileName}#${afectedLines}`; |
| 75 | + |
| 76 | + if (noterefs[key]) { |
| 77 | + if (!noterefs[key].includes(ref)) { |
| 78 | + noterefs[key].push(ref); |
| 79 | + } |
| 80 | + } else { |
| 81 | + noterefs[key] = [ref]; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + for (const desc of Object.keys(noterefs).sort()) { |
| 88 | + out.appendLine(`${desc}`); |
| 89 | + for (const ref of Object.values(noterefs[desc])) { |
| 90 | + if (baseURL) { |
| 91 | + out.appendLine(`\t ${new URL(baseURL + ref)}`); |
| 92 | + } else { |
| 93 | + out.appendLine(`\t- ${ref}`); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments