Skip to content

Commit

Permalink
feat: open clangd user/project config
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Apr 23, 2021
1 parent 06e6907 commit c9b3c58
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@
"command": "clangd.memoryUsage",
"title": "Show memory usage"
},
{
"command": "clangd.projectConfig",
"title": "Open project configuration file"
},
{
"command": "clangd.userConfig",
"title": "Open user configuration file"
},
{
"command": "clangd.install",
"title": "Install latest clangd language server binary release"
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cmds from './cmds';
import { Ctx } from './ctx';
import { FileStatus, Status } from './file_status';
import * as install from './install';
import * as openConfig from './open-config';
import { ReloadFeature } from './reload';

export async function activate(context: ExtensionContext): Promise<void> {
Expand All @@ -28,8 +29,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
return;
}

openConfig.activate(context);
const fileStatus = new FileStatus();

context.subscriptions.push(
fileStatus,

Expand Down
52 changes: 52 additions & 0 deletions src/open-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { commands, ExtensionContext, Uri, window, workspace } from 'coc.nvim';
import * as fs from 'fs';
import * as path from 'path';

function getUserConfigFile(): string {
let dir: string;
switch (process.platform) {
case 'win32':
dir = process.env.LOCALAPPDATA!;
break;
case 'darwin':
dir = path.join(process.env.HOME!, 'Library', 'Preferences');
break;
default:
dir = process.env.XDG_CONFIG_HOME || path.join(process.env.HOME!, '.config');
break;
}
if (!dir) return '';
return path.join(dir, 'clangd', 'config.yaml');
}

async function openConfigFile(p: string) {
if (!fs.existsSync(p)) {
await workspace.createFile(p);
}

await workspace.openResource(p);
}

export function activate(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand('clangd.projectConfig', () => {
if (workspace.workspaceFolders.length > 0) {
const folder = workspace.workspaceFolders[0];
openConfigFile(path.join(Uri.parse(folder.uri).fsPath, '.clangd'));
} else {
window.showMessage('No project is open', 'warning');
}
})
);

context.subscriptions.push(
commands.registerCommand('clangd.userConfig', () => {
const file = getUserConfigFile();
if (file) {
openConfigFile(file);
} else {
window.showMessage("Couldn't get global configuration directory", 'warning');
}
})
);
}

0 comments on commit c9b3c58

Please sign in to comment.