diff --git a/src/periscope.ts b/src/periscope.ts index cc34164..a5092dc 100644 --- a/src/periscope.ts +++ b/src/periscope.ts @@ -5,6 +5,7 @@ import { rgPath } from '@vscode/ripgrep'; import { highlightDecorationType } from './utils/decorationType'; import { getConfig } from './utils/getConfig'; import { getSelectedText } from './utils/getSelectedText'; +import { tryJsonParse } from './utils/tryJsonParse'; export interface QPItemDefault extends vscode.QuickPickItem { _type: 'QuickPickItemDefault' @@ -35,6 +36,16 @@ type DisposablesMap = { query: vscode.Disposable[] }; +type RgLine = { + type: string + data: { + path: { text: string } + lines: { text: string } + line_number: number + absolute_offset: number + } +}; + // Allow other commands to access the QuickPick let activeQP : vscode.QuickPick | undefined; let previousActiveEditor: vscode.TextEditor | undefined; @@ -231,8 +242,9 @@ export const periscope = () => { spawnProcess.stdout.on('data', (data: Buffer) => { const lines = data.toString().split('\n').filter(Boolean); for (const line of lines) { - const parsedLine = JSON.parse(line); - if (parsedLine.type === 'match') { + const parsedLine = tryJsonParse(line); + + if (parsedLine?.type === 'match') { const { path, lines, line_number, absolute_offset } = parsedLine.data; const filePath = path.text; const linePos = line_number; diff --git a/src/utils/tryJsonParse.ts b/src/utils/tryJsonParse.ts new file mode 100644 index 0000000..64aab3a --- /dev/null +++ b/src/utils/tryJsonParse.ts @@ -0,0 +1,7 @@ +export function tryJsonParse(meta: string): T | undefined { + try { + return JSON.parse(meta); + } catch { + return undefined; + } +} \ No newline at end of file