[clangd][vscode] clang-format the the extension code.

Summary:
As we are going to grow the extension in the near future, it is time to
formalize the TS code format/style of our extension (although we'd lose the
history).

We use default options of clang-format:
- 80 max line length
- 2 space indent

Reviewers: ilya-biryukov, sammccall, jvikstrom

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D65657

llvm-svn: 367684
This commit is contained in:
Haojian Wu 2019-08-02 14:24:02 +00:00
parent 522fb7eedc
commit 13a81444cd
5 changed files with 105 additions and 99 deletions

View File

@ -50,6 +50,11 @@ point to the binary.
# When VS Code starts, press <F5>.
```
## Contributing
Please follow the exsiting code style when contributing to the extension, we
recommend to run `npm run format` before sending a patch.
## Publish to VS Code Marketplace
New changes to `clangd-vscode` are not released until a new version is published

View File

@ -32,6 +32,7 @@
"vscode:prepublish": "tsc -p ./",
"compile": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"format": "clang-format --style=LLVM -i --glob=\"{src,test}/*.ts\"",
"test": "node ./node_modules/vscode/bin/test"
},
"dependencies": {
@ -42,6 +43,7 @@
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.40",
"mocha": "^5.2.0",
"clang-format": "1.2.4",
"typescript": "^2.0.3",
"vscode": "^1.1.0"
},

View File

@ -7,8 +7,8 @@ import * as vscodelc from 'vscode-languageclient';
* @param defaultValue default value to return if option is not set
*/
function getConfig<T>(option: string, defaultValue?: any): T {
const config = vscode.workspace.getConfiguration('clangd');
return config.get<T>(option, defaultValue);
const config = vscode.workspace.getConfiguration('clangd');
return config.get<T>(option, defaultValue);
}
namespace SwitchSourceHeaderRequest {
@ -18,35 +18,33 @@ export const type =
}
class FileStatus {
private statuses = new Map<string, any>();
private readonly statusBarItem =
vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
private statuses = new Map<string, any>();
private readonly statusBarItem =
vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
onFileUpdated(fileStatus: any) {
const filePath = vscode.Uri.parse(fileStatus.uri);
this.statuses.set(filePath.fsPath, fileStatus);
this.updateStatus();
}
onFileUpdated(fileStatus: any) {
const filePath = vscode.Uri.parse(fileStatus.uri);
this.statuses.set(filePath.fsPath, fileStatus);
this.updateStatus();
}
updateStatus() {
const path = vscode.window.activeTextEditor.document.fileName;
const status = this.statuses.get(path);
if (!status) {
this.statusBarItem.hide();
return;
}
this.statusBarItem.text = `clangd: ` + status.state;
this.statusBarItem.show();
updateStatus() {
const path = vscode.window.activeTextEditor.document.fileName;
const status = this.statuses.get(path);
if (!status) {
this.statusBarItem.hide();
return;
}
this.statusBarItem.text = `clangd: ` + status.state;
this.statusBarItem.show();
}
clear() {
this.statuses.clear();
this.statusBarItem.hide();
}
clear() {
this.statuses.clear();
this.statusBarItem.hide();
}
dispose() {
this.statusBarItem.dispose();
}
dispose() { this.statusBarItem.dispose(); }
}
/**
@ -54,27 +52,27 @@ class FileStatus {
* your extension is activated the very first time the command is executed
*/
export function activate(context: vscode.ExtensionContext) {
const syncFileEvents = getConfig<boolean>('syncFileEvents', true);
const syncFileEvents = getConfig<boolean>('syncFileEvents', true);
const clangd: vscodelc.Executable = {
command: getConfig<string>('path'),
args: getConfig<string[]>('arguments')
};
const traceFile = getConfig<string>('trace');
if (!!traceFile) {
const trace = { CLANGD_TRACE: traceFile };
clangd.options = { env: { ...process.env, ...trace } };
}
const serverOptions: vscodelc.ServerOptions = clangd;
const clangd: vscodelc.Executable = {
command : getConfig<string>('path'),
args : getConfig<string[]>('arguments')
};
const traceFile = getConfig<string>('trace');
if (!!traceFile) {
const trace = {CLANGD_TRACE : traceFile};
clangd.options = {env : {...process.env, ...trace}};
}
const serverOptions: vscodelc.ServerOptions = clangd;
// Note that CUDA ('.cu') files are special. When opening files of all other
// extensions, VSCode would load clangd automatically. This is achieved by
// having a corresponding 'onLanguage:...' activation event in package.json.
// However, VSCode does not have CUDA as a supported language yet, so we
// cannot add a corresponding activationEvent for CUDA files and clangd will
// *not* load itself automatically on '.cu' files.
const cudaFilePattern: string = '**/*.{' +['cu'].join()+ '}';
const clientOptions: vscodelc.LanguageClientOptions = {
// Note that CUDA ('.cu') files are special. When opening files of all other
// extensions, VSCode would load clangd automatically. This is achieved by
// having a corresponding 'onLanguage:...' activation event in package.json.
// However, VSCode does not have CUDA as a supported language yet, so we
// cannot add a corresponding activationEvent for CUDA files and clangd will
// *not* load itself automatically on '.cu' files.
const cudaFilePattern: string = '**/*.{' + [ 'cu' ].join() + '}';
const clientOptions: vscodelc.LanguageClientOptions = {
// Register the server for c-family and cuda files.
documentSelector: [
{ scheme: 'file', language: 'c' },
@ -91,45 +89,44 @@ export function activate(context: vscode.ExtensionContext) {
revealOutputChannelOn: vscodelc.RevealOutputChannelOn.Never
};
const clangdClient = new vscodelc.LanguageClient('Clang Language Server',serverOptions, clientOptions);
console.log('Clang Language Server is now active!');
context.subscriptions.push(clangdClient.start());
context.subscriptions.push(vscode.commands.registerCommand(
'clangd-vscode.switchheadersource', async () => {
const uri =
vscode.Uri.file(vscode.window.activeTextEditor.document.fileName);
if (!uri) {
return;
}
const docIdentifier =
vscodelc.TextDocumentIdentifier.create(uri.toString());
const sourceUri = await clangdClient.sendRequest(
SwitchSourceHeaderRequest.type, docIdentifier);
if (!sourceUri) {
return;
}
const doc = await vscode.workspace.openTextDocument(
vscode.Uri.parse(sourceUri));
vscode.window.showTextDocument(doc);
}));
const status = new FileStatus();
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(() => {
status.updateStatus();
}));
clangdClient.onDidChangeState(
({ newState }) => {
if (newState == vscodelc.State.Running) {
// clangd starts or restarts after crash.
clangdClient.onNotification(
'textDocument/clangd.fileStatus',
(fileStatus) => { status.onFileUpdated(fileStatus); });
} else if (newState == vscodelc.State.Stopped) {
// Clear all cached statuses when clangd crashes.
status.clear();
}
})
// An empty place holder for the activate command, otherwise we'll get an
// "command is not registered" error.
context.subscriptions.push(vscode.commands.registerCommand(
'clangd-vscode.activate', async () => {}));
const clangdClient = new vscodelc.LanguageClient(
'Clang Language Server', serverOptions, clientOptions);
console.log('Clang Language Server is now active!');
context.subscriptions.push(clangdClient.start());
context.subscriptions.push(vscode.commands.registerCommand(
'clangd-vscode.switchheadersource', async () => {
const uri =
vscode.Uri.file(vscode.window.activeTextEditor.document.fileName);
if (!uri) {
return;
}
const docIdentifier =
vscodelc.TextDocumentIdentifier.create(uri.toString());
const sourceUri = await clangdClient.sendRequest(
SwitchSourceHeaderRequest.type, docIdentifier);
if (!sourceUri) {
return;
}
const doc = await vscode.workspace.openTextDocument(
vscode.Uri.parse(sourceUri));
vscode.window.showTextDocument(doc);
}));
const status = new FileStatus();
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(
() => { status.updateStatus(); }));
clangdClient.onDidChangeState(({newState}) => {
if (newState == vscodelc.State.Running) {
// clangd starts or restarts after crash.
clangdClient.onNotification(
'textDocument/clangd.fileStatus',
(fileStatus) => { status.onFileUpdated(fileStatus); });
} else if (newState == vscodelc.State.Stopped) {
// Clear all cached statuses when clangd crashes.
status.clear();
}
})
// An empty place holder for the activate command, otherwise we'll get an
// "command is not registered" error.
context.subscriptions.push(vscode.commands.registerCommand(
'clangd-vscode.activate', async () => {}));
}

View File

@ -6,10 +6,9 @@ import * as myExtension from '../src/extension';
// TODO: add tests
suite("Extension Tests", () => {
// Defines a Mocha unit test
test("Something 1", () => {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
});
// Defines a Mocha unit test
test("Something 1", () => {
assert.equal(-1, [ 1, 2, 3 ].indexOf(5));
assert.equal(-1, [ 1, 2, 3 ].indexOf(0));
});
});

View File

@ -5,18 +5,21 @@
// By default the test runner in use is Mocha based.
//
// You can provide your own test runner if you want to override it by exporting
// a function run(testRoot: string, clb: (error:Error) => void) that the extension
// host can call to run the tests. The test runner is expected to use console.log
// to report the results back to the caller. When the tests are finished, return
// a possible error to the callback or null if none.
// a function run(testRoot: string, clb: (error:Error) => void) that the
// extension host can call to run the tests. The test runner is expected to use
// console.log to report the results back to the caller. When the tests are
// finished, return a possible error to the callback or null if none.
var testRunner = require('vscode/lib/testrunner');
// You can directly control Mocha options by uncommenting the following lines
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
// See
// https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options
// for more info
testRunner.configure({
ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
useColors: true // colored output from test results
ui : 'tdd', // the TDD UI is being used in extension.test.ts (suite, test,
// etc.)
useColors : true // colored output from test results
});
module.exports = testRunner;