-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathextension.ts
More file actions
142 lines (113 loc) · 4.75 KB
/
Copy pathextension.ts
File metadata and controls
142 lines (113 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as an from './runner';
import { Lint } from './lint';
import { analysisResult } from './lint'
import * as configuration from './configuration'
import { ConfigManager } from './configuration';
let outputChannel: vscode.OutputChannel;
let statusItem: vscode.StatusBarItem;
let timer: NodeJS.Timer;
let diagnosticCollection: vscode.DiagnosticCollection = vscode.languages.createDiagnosticCollection('cpplint');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
outputChannel = vscode.window.createOutputChannel('CppLint');
// outputChannel.appendLine('CppLint is running.');
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "cpplint" is now active!');
loadConfigure();
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let single = vscode.commands.registerCommand('cpplint.runAnalysis', runAnalysis);
context.subscriptions.push(single);
let whole = vscode.commands.registerCommand('cpplint.runWholeAnalysis', runWholeAnalysis);
context.subscriptions.push(whole);
let demand = vscode.commands.registerCommand('cpplint.lintCurrentFile', lintCurrentFile);
context.subscriptions.push(demand);clearLints
let clear = vscode.commands.registerCommand('cpplint.clearLints', clearLints);
context.subscriptions.push(clear);
vscode.workspace.onDidChangeConfiguration((()=>loadConfigure()).bind(this));
}
function runAnalysis(): Promise<void> {
var edit = vscode.window.activeTextEditor;
if (edit == undefined) {
return Promise.reject("no edit opened");
}
outputChannel.show();
outputChannel.clear();
let start = 'CppLint started: ' + new Date().toString();
outputChannel.appendLine(start);
let result = an.runOnFile();
outputChannel.appendLine(result);
let end = 'CppLint ended: ' + new Date().toString();
outputChannel.appendLine(end);
// vscode.window.showInformationMessage(edit.document.uri.fsPath)
return Promise.resolve()
}
function runWholeAnalysis(): Promise<void> {
outputChannel.show();
outputChannel.clear();
let start = 'CppLint started: ' + new Date().toString();
outputChannel.appendLine(start);
let result = an.runOnWorkspace();
outputChannel.appendLine(result);
let end = 'CppLint ended: ' + new Date().toString();
outputChannel.appendLine(end);
// vscode.window.showInformationMessage(edit.document.uri.fsPath)
return Promise.resolve()
}
// this method is called when your extension is deactivated
export function deactivate() {
clearTimeout(timer)
vscode.window.showInformationMessage("Cpplint deactivated")
}
function doLint() {
if (vscode.window.activeTextEditor) {
let language = vscode.window.activeTextEditor.document.languageId
if (ConfigManager.getInstance().isSupportLanguage(language)) {
if (ConfigManager.getInstance().isWorkspaceMode()) {
Lint(diagnosticCollection, true);
} else {
Lint(diagnosticCollection, false);
}
}
}
clearTimeout(timer)
}
function startLint() {
timer = global.setTimeout(doLint, 1.5 * 1000);
}
function startLint2() {
timer = global.setTimeout(doLint, 500);
}
function loadConfigure() {
ConfigManager.getInstance().initialize();
if (ConfigManager.getInstance().isSingleMode()) {
startLint2();
vscode.window.onDidChangeActiveTextEditor((() => startLint2()).bind(this));
vscode.workspace.onDidSaveTextDocument((() => startLint2()).bind(this));
} else if (ConfigManager.getInstance().isWorkspaceMode()) {
// start timer to do workspace lint
startLint();
vscode.workspace.onDidSaveTextDocument((() => startLint()).bind(this));
} else {
// Do nothing in demand mode.
}
}
// Lints current file on demand. If the file is dirty, saves it to avoid wrong
// linting as cpplint parses saved file.
function lintCurrentFile() {
if (vscode.window.activeTextEditor.document.isDirty){
vscode.window.activeTextEditor.document.save();
}
startLint2();
}
// Clears all problems and squiggles from the views.
function clearLints() {
diagnosticCollection.clear();
}