Skip to content

Commit 61954f3

Browse files
authored
Merge pull request #41 from owent-contrib/master
add support for proto3 and add alias for languages
2 parents 84fcc9e + 150086a commit 61954f3

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"JavaScript",
2323
"Objective-C",
2424
"Clang",
25-
"LLVM"
25+
"LLVM",
26+
"Protobuf"
2627
],
2728
"main": "./out/src/extension",
2829
"activationEvents": [
@@ -34,6 +35,7 @@
3435
"onLanguage:javascript",
3536
"onLanguage:typescript",
3637
"onLanguage:proto",
38+
"onLanguage:proto3",
3739
"onLanguage:apex"
3840
],
3941
"contributes": {

src/clangMode.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import vscode = require('vscode');
44

5+
export const ALIAS = {
6+
'proto3': 'proto'
7+
};
8+
59
let languages: string[] = [];
6-
for (let l of ['cpp', 'c', 'objective-c', 'objective-cpp', 'java', 'javascript', 'typescript', 'proto', 'apex']) {
7-
if (vscode.workspace.getConfiguration('clang-format').get('language.' + l + '.enable')) {
10+
for (let l of ['cpp', 'c', 'objective-c', 'objective-cpp', 'java', 'javascript', 'typescript', 'proto', 'proto3', 'apex']) {
11+
let confKey = `language.${ALIAS[l] || l}.enable`;
12+
if (vscode.workspace.getConfiguration('clang-format').get(confKey)) {
813
languages.push(l);
914
}
1015
}
1116

12-
export const MODES: vscode.DocumentFilter[] = languages.map((language) => ({ language, scheme: 'file' }));
17+
export const MODES: vscode.DocumentFilter[] = languages.map((language) => ({language, scheme: 'file'}));

src/extension.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as vscode from 'vscode';
22
import cp = require('child_process');
33
import path = require('path');
4-
import { MODES } from './clangMode';
5-
import { getBinPath } from './clangPath';
4+
import {MODES,
5+
ALIAS} from './clangMode';
6+
import {getBinPath} from './clangPath';
67
import sax = require('sax');
78

89
export let outputChannel = vscode.window.createOutputChannel('Clang-Format');
@@ -41,7 +42,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
4142
byte: 0,
4243
offset: 0
4344
};
44-
let byteToOffset = function (editInfo: { length: number, offset: number }) {
45+
let byteToOffset = function(editInfo: { length: number, offset: number }) {
4546
let offset = editInfo.offset;
4647
let length = editInfo.length;
4748

@@ -70,20 +71,20 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
7071
}
7172

7273
switch (tag.name) {
73-
case 'replacements':
74-
return;
75-
76-
case 'replacement':
77-
currentEdit = {
78-
length: parseInt(tag.attributes['length'].toString()),
79-
offset: parseInt(tag.attributes['offset'].toString()),
80-
text: ''
81-
};
82-
byteToOffset(currentEdit);
83-
break;
84-
85-
default:
86-
reject(`Unexpected tag ${tag.name}`);
74+
case 'replacements':
75+
return;
76+
77+
case 'replacement':
78+
currentEdit = {
79+
length: parseInt(tag.attributes['length'].toString()),
80+
offset: parseInt(tag.attributes['offset'].toString()),
81+
text: ''
82+
};
83+
byteToOffset(currentEdit);
84+
break;
85+
86+
default:
87+
reject(`Unexpected tag ${tag.name}`);
8788
}
8889

8990
};
@@ -133,8 +134,12 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
133134
});
134135
}
135136

137+
private getLanguage(document: vscode.TextDocument): string {
138+
return ALIAS[document.languageId] || document.languageId;
139+
}
140+
136141
private getStyle(document: vscode.TextDocument) {
137-
let ret = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${document.languageId}.style`);
142+
let ret = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${this.getLanguage(document)}.style`);
138143
if (ret.trim()) {
139144
return ret.trim();
140145
}
@@ -148,7 +153,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
148153
}
149154

150155
private getFallbackStyle(document: vscode.TextDocument) {
151-
let strConf = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${document.languageId}.fallbackStyle`);
156+
let strConf = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${this.getLanguage(document)}.fallbackStyle`);
152157
if (strConf.trim()) {
153158
return strConf;
154159
}

0 commit comments

Comments
 (0)