Skip to content

Commit

Permalink
clang-format is working
Browse files Browse the repository at this point in the history
  • Loading branch information
xaverh committed Nov 27, 2015
1 parent a9f356e commit 6fbec2d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
"categories": [
"Other"
],
"main": "./out/src/extension",
"activationEvents": [
"onCommand:extension.sayHello"
"onLanguage:cpp"
],
"main": "./out/src/extension",
"contributes": {
"commands": [{
"command": "extension.sayHello",
"title": "Hello World"
}]
"configuration": {
"type": "object",
"title": "Clang-Format configuration",
"properties": {
"clang-format.formatOnSave": {
"type": "boolean",
"default": false,
"description": "Run 'clang-format' on save."
}
}
}
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
Expand Down
5 changes: 5 additions & 0 deletions src/clangMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

import vscode = require('vscode');

export const CLANG_MODE: vscode.DocumentFilter = { language: 'cpp', scheme: 'file' }
33 changes: 33 additions & 0 deletions src/clangPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

import fs = require('fs');
import path = require('path');

var binPathCache: { [bin: string]: string; } = {}

export function getBinPath(binname: string) {
binname = correctBinname(binname);
if (binPathCache[binname]) return binPathCache[binname];

if (process.env["PATH"]) {
var pathparts = process.env["PATH"].split(path.delimiter);
for (var i = 0; i < pathparts.length; i++) {
let binpath = path.join(pathparts[i], binname);
if (fs.existsSync(binpath)) {
binPathCache[binname] = binpath;
return binpath;
}
}
}

// Else return the binary name directly (this will likely always fail downstream)
binPathCache[binname] = binname;
return binname;
}

function correctBinname(binname: string) {
if (process.platform === 'win32')
return binname + ".exe";
else
return binname
}
71 changes: 66 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as vscode from 'vscode';
import cp = require('child_process');
import path = require('path');
import { CLANG_MODE } from './clangMode';
import { getBinPath } from './clangPath';

export class ClangDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
private formatCommand = 'clang-format';

/*
constructor() {
let formatTool = vscode.workspace.getConfiguration('cpp')['formatTool'];
if (formatTool) {
this.formatCommand = formatTool;
}
}
*/

public provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
return document.save().then(() => {
return this.doFormatDocument(document, options, token);
});
}

private doFormatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
return new Promise((resolve, reject) => {
var filename = document.fileName;

var formatCommandBinPath = getBinPath(this.formatCommand);

cp.execFile(formatCommandBinPath, [filename], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.window.showInformationMessage("The '" + formatCommandBinPath + "' command is not available. Please check your go.formatTool user setting and ensure it is installed.");
return resolve(null);
}
if (err) return reject("Cannot format due to syntax errors.");
var text = stdout.toString();
// TODO: Should use `-d` option to get a diff and then compute the
// specific edits instead of replace whole buffer
var lastLine = document.lineCount;
var lastLineLastCol = document.lineAt(lastLine - 1).range.end.character;
var range = new vscode.Range(0, 0, lastLine - 1, lastLineLastCol);
return resolve([new vscode.TextEdit(range, text)]);
} catch (e) {
reject(e);
}
});
});
}

}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/*
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "clang-format" is now active!');
console.log('Your extension "clang-format" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
Expand All @@ -19,6 +68,18 @@ export function activate(context: vscode.ExtensionContext) {
// Display a message box to the user
vscode.window.showInformationMessage('Hello World!');
});
context.subscriptions.push(disposable);
}
*/

let diagnosticCollection: vscode.DiagnosticCollection;

export function activate(ctx: vscode.ExtensionContext): void {

ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(CLANG_MODE, new ClangDocumentFormattingEditProvider()));

}

function deactivate() {
}

0 comments on commit 6fbec2d

Please sign in to comment.