Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit ce0b4a5

Browse files
committed
Update to Linter v2 and use ranges
Update to the v2 Linter API. Also moves to generating a range for errors when clang doesn't provide us one in the first place, leading to a _much_ better user experience.
1 parent 9f4a6e4 commit ce0b4a5

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

lib/main.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CompositeDisposable } from 'atom';
55

66
let helpers = null;
77
let clangFlags = null;
8+
const regex = /(.+):(\d+):(\d+):(?:{(\d+):(\d+)-(\d+):(\d+)}.*:)? ([\w \\-]+): (.*)/g;
89

910
export default {
1011
activate() {
@@ -57,11 +58,10 @@ export default {
5758
},
5859

5960
provideLinter() {
60-
const regex = '(?<file>.+):(?<line>\\d+):(?<col>\\d+):({(?<lineStart>\\d+):(?<colStart>\\d+)-(?<lineEnd>\\d+):(?<colEnd>\\d+)}.*:)? (?<type>[\\w \\-]+): (?<message>.*)';
6161
return {
6262
name: 'clang',
6363
scope: 'file',
64-
lintOnFly: false,
64+
lintsOnChange: false,
6565
grammarScopes: ['source.c', 'source.cpp', 'source.objc', 'source.objcpp'],
6666
lint: async (editor) => {
6767
if (helpers === null) {
@@ -146,7 +146,32 @@ export default {
146146
return null;
147147
}
148148

149-
return helpers.parse(output, regex);
149+
const toReturn = [];
150+
let match = regex.exec(output);
151+
152+
while (match !== null) {
153+
let position;
154+
if (match[4] !== undefined) {
155+
const lineStart = Number.parseInt(match[4], 10) - 1;
156+
const colStart = Number.parseInt(match[5], 10) - 1;
157+
const lineEnd = Number.parseInt(match[6], 10) - 1;
158+
const colEnd = Number.parseInt(match[7], 10) - 1;
159+
position = [[lineStart, colStart], [lineEnd, colEnd]];
160+
} else {
161+
const line = Number.parseInt(match[2], 10) - 1;
162+
const col = Number.parseInt(match[3], 10) - 1;
163+
position = helpers.generateRange(editor, line, col);
164+
}
165+
const severity = /error/.test(match[8]) ? 'error' : 'warning';
166+
toReturn.push({
167+
severity,
168+
location: { file: match[1], position },
169+
excerpt: match[9],
170+
});
171+
match = regex.exec(output);
172+
}
173+
174+
return toReturn;
150175
},
151176
};
152177
},

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
"providedServices": {
5555
"linter": {
5656
"versions": {
57-
"1.0.0": "provideLinter"
57+
"2.0.0": "provideLinter"
5858
}
5959
}
6060
},
6161
"dependencies": {
6262
"atom-linter": "^9.0.0",
63-
"atom-package-deps": "^4.0.1",
63+
"atom-package-deps": "^4.5.0",
6464
"clang-flags": "^0.2.2"
6565
},
6666
"devDependencies": {
@@ -70,7 +70,7 @@
7070
"jasmine-fix": "^1.0.1"
7171
},
7272
"package-deps": [
73-
"linter"
73+
"linter:2.0.0"
7474
],
7575
"eslintConfig": {
7676
"extends": "airbnb-base",

spec/linter-clang-spec.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,40 @@ describe('The Clang provider for AtomLinter', () => {
2626
const editor = await atom.workspace.open(`${miPath}.c`);
2727
const messages = await lint(editor);
2828
expect(messages.length).toBe(1);
29-
expect(messages[0].type).toBe('fatal error');
30-
expect(messages[0].text).toBe("'nothing.h' file not found");
29+
expect(messages[0].severity).toBe('error');
30+
expect(messages[0].excerpt).toBe("'nothing.h' file not found");
31+
expect(messages[0].location.file).toBe(`${miPath}.c`);
32+
expect(messages[0].location.position).toEqual([[1, 9], [1, 17]]);
3133
});
3234

3335
it('finds a fatal error in "missing_import.cpp"', async () => {
3436
const editor = await atom.workspace.open(`${miPath}.cpp`);
3537
const messages = await lint(editor);
3638
expect(messages.length).toBe(1);
37-
expect(messages[0].type).toEqual('fatal error');
38-
expect(messages[0].text).toEqual("'nothing.h' file not found");
39+
expect(messages[0].severity).toBe('error');
40+
expect(messages[0].excerpt).toBe("'nothing.h' file not found");
41+
expect(messages[0].location.file).toBe(`${miPath}.cpp`);
42+
expect(messages[0].location.position).toEqual([[1, 9], [1, 17]]);
3943
});
4044

4145
it('finds a fatal error in "missing_import.m"', async () => {
4246
const editor = await atom.workspace.open(`${miPath}.m`);
4347
const messages = await lint(editor);
4448
expect(messages.length).toBe(1);
45-
expect(messages[0].type).toEqual('fatal error');
46-
expect(messages[0].text).toEqual("'nothing.h' file not found");
49+
expect(messages[0].severity).toBe('error');
50+
expect(messages[0].excerpt).toBe("'nothing.h' file not found");
51+
expect(messages[0].location.file).toBe(`${miPath}.m`);
52+
expect(messages[0].location.position).toEqual([[1, 9], [1, 17]]);
4753
});
4854

4955
it('finds a fatal error in "missing_import.mm"', async () => {
5056
const editor = await atom.workspace.open(`${miPath}.mm`);
5157
const messages = await lint(editor);
5258
expect(messages.length).toBe(1);
53-
expect(messages[0].type).toEqual('fatal error');
54-
expect(messages[0].text).toEqual("'nothing.h' file not found");
59+
expect(messages[0].severity).toBe('error');
60+
expect(messages[0].excerpt).toBe("'nothing.h' file not found");
61+
expect(messages[0].location.file).toBe(`${miPath}.mm`);
62+
expect(messages[0].location.position).toEqual([[1, 9], [1, 17]]);
5563
});
5664
});
5765
});

0 commit comments

Comments
 (0)