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

Commit d1ea2b5

Browse files
authored
Merge pull request #193 from AtomLinter/arcanemagus/lintOnChange
Enable linting on change
2 parents 95d152e + 8889b1d commit d1ea2b5

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lib/main.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default {
7373
return {
7474
name: 'clang',
7575
scope: 'file',
76-
lintsOnChange: false,
76+
lintsOnChange: true,
7777
grammarScopes: ['source.c', 'source.cpp', 'source.objc', 'source.objcpp'],
7878
lint: async (editor) => {
7979
if (helpers === null) {
@@ -84,7 +84,13 @@ export default {
8484
}
8585

8686
const filePath = editor.getPath();
87+
if (filePath === undefined) {
88+
// The editor has no path, meaning it hasn't been saved. Although
89+
// clang could give us results for this, Linter needs a path
90+
return [];
91+
}
8792
const fileExt = extname(filePath);
93+
const fileDir = dirname(filePath);
8894
const fileText = editor.getText();
8995

9096
const args = [
@@ -96,6 +102,7 @@ export default {
96102
`-ferror-limit=${this.clangErrorLimit}`,
97103
];
98104

105+
// Non-Public API!
99106
const grammar = editor.getGrammar().name;
100107

101108
switch (grammar) {
@@ -148,17 +155,13 @@ export default {
148155
}
149156
}
150157

151-
args.push(filePath);
152-
153-
let [projectPath] = atom.project.relativizePath(filePath);
154-
if (projectPath === null) {
155-
projectPath = dirname(filePath);
156-
}
158+
args.push('-');
157159

158160
const execOpts = {
161+
stdin: fileText,
159162
stream: 'stderr',
160163
allowEmptyStderr: true,
161-
cwd: projectPath,
164+
cwd: fileDir,
162165
};
163166

164167
const output = await helpers.exec(this.executablePath, args, execOpts);
@@ -187,9 +190,11 @@ export default {
187190
position = helpers.generateRange(editor, line, col);
188191
}
189192
const severity = /error/.test(match[8]) ? 'error' : 'warning';
193+
// Handle paths other than the file being linted still
194+
const file = match[1] === '<stdin>' ? filePath : match[1];
190195
toReturn.push({
191196
severity,
192-
location: { file: match[1], position },
197+
location: { file, position },
193198
excerpt: match[9],
194199
});
195200
match = regex.exec(output);

spec/linter-clang-spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const lint = require('../lib/main').provideLinter().lint;
99
const miPath = join(__dirname, 'files', 'missing_import');
1010
const poPath = join(__dirname, 'files', 'pragma', 'pragma_once');
1111
const validPath = join(__dirname, 'files', 'valid.c');
12+
const fileText = `// This is a comment, this will not return any errors.
13+
#include "nothing.h"
14+
15+
int main(int argc, char const *argv[]) {
16+
/* code */
17+
return 0;
18+
}
19+
`;
1220

1321
describe('The Clang provider for AtomLinter', () => {
1422
beforeEach(async () => {
@@ -97,4 +105,18 @@ describe('The Clang provider for AtomLinter', () => {
97105
expect(messages.length).toEqual(0);
98106
});
99107
});
108+
109+
it('works on a modified file', async () => {
110+
// Open the valid file
111+
const editor = await atom.workspace.open(validPath);
112+
// Set the text to invalid text
113+
editor.setText(fileText);
114+
// Lint the editor
115+
const messages = await lint(editor);
116+
expect(messages.length).toBe(1);
117+
expect(messages[0].severity).toBe('error');
118+
expect(messages[0].excerpt).toBe("'nothing.h' file not found");
119+
expect(messages[0].location.file).toBe(validPath);
120+
expect(messages[0].location.position).toEqual([[1, 9], [1, 20]]);
121+
});
100122
});

0 commit comments

Comments
 (0)