Skip to content

Commit

Permalink
1. support proto3(works with vscode-proto3)
Browse files Browse the repository at this point in the history
2. add alias for languages.
  • Loading branch information
owent committed May 24, 2017
1 parent 7ef4194 commit 150086a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"JavaScript",
"Objective-C",
"Clang",
"LLVM"
"LLVM",
"Protobuf"
],
"main": "./out/src/extension",
"activationEvents": [
Expand All @@ -34,6 +35,7 @@
"onLanguage:javascript",
"onLanguage:typescript",
"onLanguage:proto",
"onLanguage:proto3",
"onLanguage:apex"
],
"contributes": {
Expand Down
11 changes: 8 additions & 3 deletions src/clangMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import vscode = require('vscode');

export const ALIAS = {
'proto3': 'proto'
};

let languages: string[] = [];
for (let l of ['cpp', 'c', 'objective-c', 'objective-cpp', 'java', 'javascript', 'typescript', 'proto', 'apex']) {
if (vscode.workspace.getConfiguration('clang-format').get('language.' + l + '.enable')) {
for (let l of ['cpp', 'c', 'objective-c', 'objective-cpp', 'java', 'javascript', 'typescript', 'proto', 'proto3', 'apex']) {
let confKey = `language.${ALIAS[l] || l}.enable`;
if (vscode.workspace.getConfiguration('clang-format').get(confKey)) {
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'}));
43 changes: 24 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from 'vscode';
import cp = require('child_process');
import path = require('path');
import { MODES } from './clangMode';
import { getBinPath } from './clangPath';
import {MODES,
ALIAS} from './clangMode';
import {getBinPath} from './clangPath';
import sax = require('sax');

export let outputChannel = vscode.window.createOutputChannel('Clang-Format');
Expand Down Expand Up @@ -41,7 +42,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
byte: 0,
offset: 0
};
let byteToOffset = function (editInfo: { length: number, offset: number }) {
let byteToOffset = function(editInfo: { length: number, offset: number }) {
let offset = editInfo.offset;
let length = editInfo.length;

Expand Down Expand Up @@ -70,20 +71,20 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
}

switch (tag.name) {
case 'replacements':
return;

case 'replacement':
currentEdit = {
length: parseInt(tag.attributes['length'].toString()),
offset: parseInt(tag.attributes['offset'].toString()),
text: ''
};
byteToOffset(currentEdit);
break;

default:
reject(`Unexpected tag ${tag.name}`);
case 'replacements':
return;

case 'replacement':
currentEdit = {
length: parseInt(tag.attributes['length'].toString()),
offset: parseInt(tag.attributes['offset'].toString()),
text: ''
};
byteToOffset(currentEdit);
break;

default:
reject(`Unexpected tag ${tag.name}`);
}

};
Expand Down Expand Up @@ -133,8 +134,12 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
});
}

private getLanguage(document: vscode.TextDocument): string {
return ALIAS[document.languageId] || document.languageId;
}

private getStyle(document: vscode.TextDocument) {
let ret = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${document.languageId}.style`);
let ret = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${this.getLanguage(document)}.style`);
if (ret.trim()) {
return ret.trim();
}
Expand All @@ -148,7 +153,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
}

private getFallbackStyle(document: vscode.TextDocument) {
let strConf = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${document.languageId}.fallbackStyle`);
let strConf = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${this.getLanguage(document)}.fallbackStyle`);
if (strConf.trim()) {
return strConf;
}
Expand Down

0 comments on commit 150086a

Please sign in to comment.