Skip to content

Commit

Permalink
fix: rg line json parse error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmu committed Apr 3, 2024
1 parent 04bf156 commit 1c4d958
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/periscope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<AllQPItemVariants> | undefined;
let previousActiveEditor: vscode.TextEditor | undefined;
Expand Down Expand Up @@ -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<RgLine>(line);

if (parsedLine?.type === 'match') {
const { path, lines, line_number, absolute_offset } = parsedLine.data;
const filePath = path.text;
const linePos = line_number;
Expand Down
7 changes: 7 additions & 0 deletions src/utils/tryJsonParse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function tryJsonParse<T>(meta: string): T | undefined {
try {
return JSON.parse(meta);
} catch {
return undefined;
}
}

0 comments on commit 1c4d958

Please sign in to comment.