Skip to content

Commit 39ef27c

Browse files
committed
Allow user to select font from dropdown
1 parent 8a98618 commit 39ef27c

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
{ "command": "csv.toggleExtension", "title": "CSV: Toggle Extension On/Off" },
4040
{ "command": "csv.toggleHeader", "title": "CSV: Toggle First Row as Header" },
4141
{ "command": "csv.toggleSerialIndex", "title": "CSV: Toggle Serial Index Column" },
42-
{ "command": "csv.changeSeparator", "title": "CSV: Change CSV Separator" }
42+
{ "command": "csv.changeSeparator", "title": "CSV: Change CSV Separator" },
43+
{ "command": "csv.changeFontFamily", "title": "CSV: Change Font Family"}
4344
],
4445
"configuration": {
4546
"type": "object",
@@ -87,7 +88,14 @@
8788
}
8889
]
8990
}
90-
]
91+
],
92+
"csv.fontFamily": {
93+
"type": "string",
94+
"default": "",
95+
"description": "Font family used by the CSV custom editor. Leave empty to inherit ‘editor.fontFamily’.",
96+
"scope": "application"
97+
}
98+
9199
},
92100
"scripts": {
93101
"compile": "tsc -p ./",
@@ -96,7 +104,8 @@
96104
"package": "vsce package"
97105
},
98106
"dependencies": {
99-
"papaparse": "^5.4.1"
107+
"papaparse": "^5.4.1",
108+
"font-list": "^1.5.1"
100109
},
101110
"devDependencies": {
102111
"@types/mocha": "^10.0.10",

src/extension.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getFonts } from 'font-list';
12
import Papa from 'papaparse';
23
import * as vscode from 'vscode';
34

@@ -37,7 +38,41 @@ export function activate(context: vscode.ExtensionContext) {
3738
vscode.window.showInformationMessage(`CSV separator changed to "${input}"`);
3839
CsvEditorProvider.editors.forEach(editor => editor.refresh());
3940
}
41+
}),
42+
vscode.commands.registerCommand('csv.changeFontFamily', async () => {
43+
const csvCfg = vscode.workspace.getConfiguration('csv');
44+
const editorCfg = vscode.workspace.getConfiguration('editor');
45+
46+
const currentCsvFont = csvCfg.get<string>('fontFamily', '');
47+
const inheritedFont = editorCfg.get<string>('fontFamily', 'Menlo');
48+
const currentEffective = currentCsvFont || inheritedFont;
49+
50+
// Build QuickPick list
51+
let fonts: string[] = [];
52+
try {
53+
fonts = (await getFonts()).map((f: string) => f.replace(/^"(.*)"$/, '$1')).sort();
54+
} catch (e) {
55+
console.error('CSV: unable to enumerate system fonts', e);
56+
}
57+
const picks = ['(inherit editor setting)', ...fonts];
58+
59+
const choice = await vscode.window.showQuickPick(picks, {
60+
placeHolder: `Current: ${currentEffective}`
61+
});
62+
if (choice === undefined) { return; } // user aborted
63+
64+
const newVal = choice === '(inherit editor setting)' ? '' : choice;
65+
await csvCfg.update('fontFamily', newVal, vscode.ConfigurationTarget.Global);
66+
67+
vscode.window.showInformationMessage(
68+
newVal
69+
? `CSV font set to “${newVal}”.`
70+
: 'CSV font now inherits editor.fontFamily.'
71+
);
72+
// Refresh all open CSV editors
73+
CsvEditorProvider.editors.forEach(ed => ed.refresh());
4074
})
75+
4176
);
4277

4378
// Register the custom editor provider for CSV files.

src/types/font-list.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// src/types/font-list.d.ts
2+
declare module 'font-list' {
3+
/** Returns the list of installed font family names. */
4+
export function getFonts(): Promise<string[]>;
5+
}

0 commit comments

Comments
 (0)