Skip to content

Commit 5edee75

Browse files
committed
good so far
1 parent b4611d4 commit 5edee75

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

Diff for: package-lock.json

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+4
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@
4949
"devDependencies": {
5050
"@types/mocha": "^10.0.6",
5151
"@types/node": "18.x",
52+
"@types/papaparse": "^5.3.14",
5253
"@types/vscode": "^1.94.0",
5354
"@typescript-eslint/eslint-plugin": "^7.4.0",
5455
"@typescript-eslint/parser": "^7.4.0",
5556
"@vscode/test-cli": "^0.0.8",
5657
"@vscode/test-electron": "^2.3.9",
5758
"eslint": "^8.57.0",
5859
"typescript": "^5.3.3"
60+
},
61+
"dependencies": {
62+
"papaparse": "^5.4.1"
5963
}
6064
}

Diff for: src/csvParser.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Papa from 'papaparse';
2+
13
export function CSVParse(
24
stringFromDoc: string,
35
delimiter: string
@@ -9,4 +11,31 @@ export function CSVParse(
911

1012
return cells;
1113

12-
}
14+
}
15+
16+
export interface CsvData {
17+
data: number[][];
18+
delimiter: string;
19+
}
20+
21+
22+
export function parseCSVWithPapa(csv: string): CsvData {
23+
let delimiter = ',';
24+
const result = Papa.parse<string[]>(csv, {
25+
delimiter: "", // Leave as empty string for auto-detect
26+
complete: function(results) {
27+
delimiter = results.meta.delimiter;
28+
}
29+
});
30+
31+
// convert cells to just their lengths
32+
const rowsOfCellLengthArrays = result.data.map(
33+
rowOfCells => (
34+
rowOfCells.map(cell => cell.length)
35+
)
36+
);
37+
38+
// `result.data` contains the parsed array of arrays
39+
return {data: rowsOfCellLengthArrays, delimiter: delimiter};
40+
}
41+

Diff for: src/extension.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
44

5-
import { CSVParse } from './csvParser';
5+
import { CSVParse, parseCSVWithPapa, CsvData } from './csvParser';
66

77
// import helper functions
88
import { getHintsFromCellLengths } from './helpers/getHintsFromCellLengths';
99
import { VsCodeInlayHintAdapter } from './helpers/getHintAndCurrentPosfromCol';
1010
import { getMaxColumnWidthsFromCellLengths } from './helpers/getMaxColumnWidthsFromCellLengths';
1111

12-
1312
// This method is called when your extension is activated
1413
// Your extension is activated the very first time the command is executed
1514
export function activate(context: vscode.ExtensionContext) {
@@ -27,42 +26,43 @@ class CsvAlignInlayHintsProvider implements vscode.InlayHintsProvider {
2726
token: vscode.CancellationToken
2827
): vscode.InlayHint[] {
2928

30-
const delimiter = ',';
3129
const hintCharacter = ' ';
3230

31+
console.log('provideInlayHints called');
32+
3333
// Read the entire document and split it into rows
3434
const stringFromDoc = document.getText(range);
35-
36-
// csv parsing
37-
const rowsOfCells = CSVParse(stringFromDoc, delimiter);
38-
39-
// convert cells to just their lengths
40-
const cellLengths = rowsOfCells.map(
41-
rowOfCells => (
42-
rowOfCells.map(cell => cell.length)
43-
)
44-
);
35+
36+
console.log('text received');
37+
const {
38+
data: rowsOfCellLengthArrays,
39+
delimiter: delimiter,
40+
}: CsvData = parseCSVWithPapa(stringFromDoc);
4541

4642
// actual business logic
4743

48-
const maxColumnWidths: number[] = getMaxColumnWidthsFromCellLengths(cellLengths);
44+
const maxColumnWidths: number[] = getMaxColumnWidthsFromCellLengths(rowsOfCellLengthArrays);
4945

5046
const hints: VsCodeInlayHintAdapter[] = getHintsFromCellLengths(
51-
cellLengths,
47+
rowsOfCellLengthArrays,
5248
maxColumnWidths,
5349
delimiter.length,
5450
hintCharacter
5551
);
5652

57-
return hints.map(hint => new vscode.InlayHint(
53+
const vsCodeHints = hints.map(hint => new vscode.InlayHint(
5854
new vscode.Position(hint.position.rowIndex, hint.position.startPos),
5955
hint.label)
6056
);
6157

58+
return vsCodeHints;
59+
6260
}
6361
}
6462

6563
// This method is called when your extension is deactivated
66-
export function deactivate() { }
64+
export function deactivate() {
65+
console.log('deactivated');
66+
}
6767

6868

Diff for: tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"target": "ES2022",
55
"outDir": "out",
66
"lib": [
7-
"ES2022"
7+
"ES2022",
8+
"DOM"
89
],
910
"sourceMap": true,
1011
"rootDir": "src",

0 commit comments

Comments
 (0)