Skip to content

Commit

Permalink
linting galore
Browse files Browse the repository at this point in the history
  • Loading branch information
xaverh committed Apr 20, 2017
1 parent 49716fb commit 1f92cc8
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/clangMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import vscode = require('vscode');

let languages: string[] = [];
for (var l of ['cpp', 'c', 'objective-c', 'objective-cpp', 'java', 'javascript', 'typescript', 'proto']) {
for (let l of ['cpp', 'c', 'objective-c', 'objective-cpp', 'java', 'javascript', 'typescript', 'proto']) {
if (vscode.workspace.getConfiguration('clang-format').get('language.' + l + '.enable')) {
languages.push(l);
}
}

export const MODES: vscode.DocumentFilter[] = languages.map(language => ({language, scheme : 'file'}));
export const MODES: vscode.DocumentFilter[] = languages.map((language) => ({language, scheme: 'file'}));
16 changes: 8 additions & 8 deletions src/clangPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import fs = require('fs');
import path = require('path');

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

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

for (let binNameToSearch of correctBinname(binname)) {
// clang-format.executable has a valid absolute path
Expand All @@ -18,8 +18,8 @@ export function getBinPath(binname: string) {
}

if (process.env['PATH']) {
var pathparts = process.env['PATH'].split(path.delimiter);
for (var i = 0; i < pathparts.length; i++) {
let pathparts = process.env['PATH'].split(path.delimiter);
for (let i = 0; i < pathparts.length; i++) {
let binpath = path.join(pathparts[i], binNameToSearch);
if (fs.existsSync(binpath)) {
binPathCache[binname] = binpath;
Expand All @@ -34,10 +34,10 @@ export function getBinPath(binname: string) {
return binname;
}

function correctBinname(binname: string): [ string ] {
function correctBinname(binname: string): [string] {
if (process.platform === 'win32') {
return [ binname + '.exe', binname + '.bat', binname + '.cmd', binname ];
return [binname + '.exe', binname + '.bat', binname + '.cmd', binname];
} else {
return [ binname ];
return [binname];
}
}
76 changes: 38 additions & 38 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import sax = require('sax');

export class ClangDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider, vscode.DocumentRangeFormattingEditProvider {
private defaultConfigure = {
executable : 'clang-format',
style : 'file',
fallbackStyle : 'none',
assumeFilename : ''
executable: 'clang-format',
style: 'file',
fallbackStyle: 'none',
assumeFilename: ''
};

public provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
Expand All @@ -23,25 +23,25 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma

private getEdits(document: vscode.TextDocument, xml: string, codeContent: string): Thenable<vscode.TextEdit[]> {
return new Promise((resolve, reject) => {
var options = {
trim : false,
normalize : false,
loose : true
let options = {
trim: false,
normalize: false,
loose: true
};
var parser = sax.parser(true, options);
let parser = sax.parser(true, options);

var edits: vscode.TextEdit[] = [];
var currentEdit: {length : number, offset : number, text : string};
let edits: vscode.TextEdit[] = [];
let currentEdit: {length: number, offset: number, text: string};

var codeBuffer = new Buffer(codeContent);
let codeBuffer = new Buffer(codeContent);
// encoding position cache
var codeByteOffsetCache = {
byte : 0,
offset : 0
let codeByteOffsetCache = {
byte: 0,
offset: 0
};
var byteToOffset = function(editInfo: {length : number, offset : number}) {
var offset = editInfo.offset;
var length = editInfo.length;
let byteToOffset = function(editInfo: {length: number, offset: number}) {
let offset = editInfo.offset;
let length = editInfo.length;

if (offset >= codeByteOffsetCache.byte) {
editInfo.offset = codeByteOffsetCache.offset + codeBuffer.slice(codeByteOffsetCache.byte, offset).toString('utf8').length;
Expand Down Expand Up @@ -73,9 +73,9 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma

case 'replacement':
currentEdit = {
length : parseInt(tag.attributes['length'].toString()),
offset : parseInt(tag.attributes['offset'].toString()),
text : ''
length: parseInt(tag.attributes['length'].toString()),
offset: parseInt(tag.attributes['offset'].toString()),
text: ''
};
byteToOffset(currentEdit);
break;
Expand All @@ -95,10 +95,10 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
parser.onclosetag = (tagName) => {
if (!currentEdit) { return; }

var start = document.positionAt(currentEdit.offset);
var end = document.positionAt(currentEdit.offset + currentEdit.length);
let start = document.positionAt(currentEdit.offset);
let end = document.positionAt(currentEdit.offset + currentEdit.length);

var editRange = new vscode.Range(start, end);
let editRange = new vscode.Range(start, end);

edits.push(new vscode.TextEdit(editRange, currentEdit.text));
currentEdit = null;
Expand Down Expand Up @@ -169,12 +169,12 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma

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

var formatCommandBinPath = getBinPath(this.getExecutablePath());
var codeContent = document.getText();
let formatCommandBinPath = getBinPath(this.getExecutablePath());
let codeContent = document.getText();

var childCompleted = (err, stdout, stderr) => {
let childCompleted = (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
vscode.window.showInformationMessage('The \'' + formatCommandBinPath + '\' command is not available. Please check your clang-format.executable user setting and ensure it is installed.');
Expand All @@ -184,7 +184,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
return reject('Cannot format due to syntax errors.');
}

var dummyProcessor = (value: string) => {
let dummyProcessor = (value: string) => {
debugger;
return value;
};
Expand All @@ -195,16 +195,16 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
}
};

var formatArgs = [
let formatArgs = [
'-output-replacements-xml',
`-style=${this.getStyle(document)}`,
`-fallback-style=${this.getFallbackStyle(document)}`,
`-assume-filename=${this.getAssumedFilename(document)}`,
`-assume-filename=${this.getAssumedFilename(document)}`
];

if (range) {
var offset = document.offsetAt(range.start);
var length = document.offsetAt(range.end) - offset;
let offset = document.offsetAt(range.start);
let length = document.offsetAt(range.end) - offset;

// fix charater length to byte length
length = Buffer.byteLength(codeContent.substr(offset, length), 'utf8');
Expand All @@ -214,12 +214,12 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
formatArgs.push(`-offset=${offset}`, `-length=${length}`);
}

var workingPath = vscode.workspace.rootPath;
let workingPath = vscode.workspace.rootPath;
if (!document.isUntitled) {
workingPath = path.dirname(document.fileName);
}

var child = cp.execFile(formatCommandBinPath, formatArgs, {cwd : workingPath}, childCompleted);
let child = cp.execFile(formatCommandBinPath, formatArgs, {cwd: workingPath}, childCompleted);
child.stdin.end(codeContent);

if (token) {
Expand All @@ -240,10 +240,10 @@ let diagnosticCollection: vscode.DiagnosticCollection;

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

var formatter = new ClangDocumentFormattingEditProvider();
var availableLanguages = {};
let formatter = new ClangDocumentFormattingEditProvider();
let availableLanguages = {};

MODES.forEach(mode => {
MODES.forEach((mode) => {
ctx.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(mode, formatter));
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(mode, formatter));
availableLanguages[mode.language] = true;
Expand Down
97 changes: 87 additions & 10 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,89 @@
{
"rules": {
"no-unused-expression": true,
"no-duplicate-variable": true,
"no-duplicate-key": true,
"no-unused-variable": true,
"curly": true,
"class-name": true,
"semicolon": ["always"],
"triple-equals": true
}
"rules": {
"array-type": [
true,
"array-simple"
],
"arrow-parens": true,
"no-var-keyword": true,
"no-unused-variable": [
true,
{
"ignore-pattern": "^_"
}
],
"ordered-imports": [
true,
{
"import-sources-order": "lowercase-last",
"named-imports-order": "lowercase-first"
}
],
"trailing-comma": [
true,
{
"multiline": "never",
"singleline": "never"
}
],
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"single"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
},
"jsRules": {
"triple-equals": [
true,
"allow-null-check"
]
},
"defaultSeverity": "warning"
}

0 comments on commit 1f92cc8

Please sign in to comment.