-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: formatting and tidying up code
- Loading branch information
1 parent
88a06bb
commit 834dfa6
Showing
2 changed files
with
151 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,21 @@ | ||
import * as vscode from "vscode"; | ||
import { GoctlDefinitionProvider } from "./goctlDeclaration"; | ||
import { GoctlDocumentFormattingEditProvider } from "./goctlFormat"; | ||
import { GOCTL } from "./goctlMode"; | ||
import * as vscode from 'vscode'; | ||
import { GOCTL } from './goctlMode'; | ||
import { GoctlDocumentFormattingEditProvider } from './goctlFormat'; | ||
import { GoctlDefinitionProvider } from './goctlDeclaration'; | ||
|
||
export let goctlOutputChannel: vscode.OutputChannel; | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
if (!goctlOutputChannel) { | ||
goctlOutputChannel = vscode.window.createOutputChannel("Goctl"); | ||
} | ||
if (process.env.PATH) { | ||
goctlOutputChannel.appendLine("$PATH:" + process.env.PATH); | ||
} | ||
|
||
registerUsualProviders(context); | ||
|
||
// 打开文件时,执行一次format | ||
if (vscode.window.activeTextEditor) { | ||
vscode.commands.executeCommand("editor.action.formatDocument"); | ||
} | ||
if (!goctlOutputChannel) { | ||
goctlOutputChannel = vscode.window.createOutputChannel('Goctl'); | ||
} | ||
if (process.env.PATH) { | ||
goctlOutputChannel.appendLine("$PATH:" + process.env.PATH); | ||
} | ||
registerUsualProviders(context); | ||
} | ||
|
||
function registerUsualProviders(context: vscode.ExtensionContext) { | ||
context.subscriptions.push( | ||
vscode.languages.registerDefinitionProvider( | ||
GOCTL, | ||
new GoctlDefinitionProvider() | ||
) | ||
); | ||
context.subscriptions.push( | ||
vscode.languages.registerDocumentFormattingEditProvider( | ||
GOCTL, | ||
new GoctlDocumentFormattingEditProvider() | ||
) | ||
); | ||
} | ||
context.subscriptions.push(vscode.languages.registerDefinitionProvider(GOCTL, new GoctlDefinitionProvider())); | ||
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GOCTL, new GoctlDocumentFormattingEditProvider())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,185 +1,138 @@ | ||
import * as vscode from "vscode"; | ||
import { goctlOutputChannel } from "./extension"; | ||
import * as util from "./util"; | ||
import cp = require("child_process"); | ||
|
||
export class GoctlDocumentFormattingEditProvider | ||
implements vscode.DocumentFormattingEditProvider | ||
{ | ||
// 创建集合 | ||
coll = vscode.languages.createDiagnosticCollection( | ||
vscode.window.activeTextEditor?.document.fileName | ||
); | ||
|
||
provideDocumentFormattingEdits( | ||
document: vscode.TextDocument, | ||
options: vscode.FormattingOptions, | ||
token: vscode.CancellationToken | ||
): vscode.ProviderResult<vscode.TextEdit[]> { | ||
document = document; | ||
token = token; | ||
options = options; | ||
return this.runFormatter(document, token).then( | ||
(edits) => edits, | ||
(err) => { | ||
if (err) { | ||
let errs = err.split("\n"); | ||
errs.forEach((element: string) => { | ||
if (element.trim().length === 0) { | ||
return; | ||
} | ||
|
||
// 创建错误提示 | ||
const errObj = this.createDiagnostic(document, element); | ||
const diagnostics: vscode.Diagnostic[] = []; | ||
|
||
const hasErr = this.coll.get(document.uri); | ||
if (hasErr) { | ||
this.coll.get(document.uri)?.forEach((item) => { | ||
if (item.message === errObj.message) { | ||
return; | ||
} else { | ||
if (item.range.start.line === errObj.range.start.line) { | ||
return; | ||
} | ||
} | ||
}); | ||
} | ||
diagnostics.push(errObj); | ||
|
||
this.coll.set(document.uri, diagnostics); | ||
|
||
// vscode.window.showErrorMessage(element) | ||
}); | ||
|
||
return Promise.reject(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
private runFormatter( | ||
// formatTool: string, | ||
// formatFlags: string[], | ||
document: vscode.TextDocument, | ||
token: vscode.CancellationToken | ||
): Thenable<vscode.TextEdit[]> { | ||
const formatFlags = [ | ||
"api", | ||
"format", | ||
"-iu", | ||
"-dir", | ||
document.fileName, | ||
"--stdin", | ||
]; | ||
return new Promise<vscode.TextEdit[]>((resolve, reject) => { | ||
let stdout = ""; | ||
let stderr = ""; | ||
|
||
const p = cp.spawn("goctl", formatFlags); | ||
|
||
token.onCancellationRequested(() => !p.killed && util.killTree(p.pid)); | ||
p.stdout.setEncoding("utf8"); | ||
p.stdout.on("data", (data) => (stdout += data)); | ||
p.stderr.on("data", (data) => (stderr += data)); | ||
p.on("error", (err) => { | ||
if (!err) { | ||
vscode.window.showWarningMessage( | ||
"Unknown mistake , please feedback." | ||
); | ||
return reject(); | ||
} | ||
goctlOutputChannel.appendLine(err.toString()); | ||
|
||
let errCode = (<any>err).code; | ||
|
||
switch (errCode) { | ||
case "ENOENT": { | ||
// promptForMissingTool(formatTool); | ||
vscode.window.showInformationMessage( | ||
'If you don\'t have goctl installed, you can install it with the following this doc: "https://github.com/zeromicro/go-zero#6-quick-start"' | ||
); | ||
vscode.window.showWarningMessage( | ||
"Check the console in goctl when formatting. goctl seem not in your $PATH , please try in terminal." | ||
); | ||
break; | ||
} | ||
case "EACCES": { | ||
vscode.window.showWarningMessage( | ||
"Check the console in goctl when formatting. goctl seem no executable permissions, please try in terminal." | ||
); | ||
break; | ||
} | ||
default: { | ||
vscode.window.showWarningMessage(err.toString()); | ||
break; | ||
} | ||
} | ||
return reject(); | ||
}); | ||
p.on("close", (code) => { | ||
if (code !== 0) { | ||
return reject(stderr); | ||
} | ||
|
||
// Return the complete file content in the edit. | ||
// VS Code will calculate minimal edits to be applied | ||
const fileStart = new vscode.Position(0, 0); | ||
const fileEnd = document.lineAt(document.lineCount - 1).range.end; | ||
const textEdits: vscode.TextEdit[] = [ | ||
new vscode.TextEdit(new vscode.Range(fileStart, fileEnd), stdout), | ||
]; | ||
|
||
// const timeTaken = Date.now() - t0; | ||
// sendTelemetryEventForFormatting(formatTool, timeTaken); | ||
// if (timeTaken > 750) { | ||
// console.log(`Formatting took too long(${timeTaken}ms). Format On Save feature could be aborted.`); | ||
// } | ||
|
||
// 删除错误提示 | ||
this.coll.delete(document.uri); | ||
|
||
return resolve(textEdits); | ||
}); | ||
if (p.pid) { | ||
p.stdin.end(document.getText()); | ||
} | ||
}); | ||
} | ||
|
||
// 创建 | ||
createDiagnostic(doc: vscode.TextDocument, err: string): vscode.Diagnostic { | ||
let errArr = err.split(" "); | ||
let [line, charStart] = errArr[2].split(":"); | ||
|
||
let source = errArr[0]; | ||
let msg = errArr.splice(3).join(" ").trim(); | ||
|
||
const lineNumber = parseInt(line) - 1; | ||
const text = doc.lineAt(lineNumber); | ||
const charNumber = text.text.trim().length; | ||
const range = new vscode.Range( | ||
new vscode.Position(lineNumber, parseInt(charStart) + 1), | ||
new vscode.Position(lineNumber, charNumber) | ||
); | ||
return { | ||
code: "", | ||
message: msg, | ||
range: range, | ||
severity: vscode.DiagnosticSeverity.Error, | ||
source: source, | ||
// relatedInformation: [ | ||
// new vscode.DiagnosticRelatedInformation( | ||
// new vscode.Location( | ||
// document.uri, | ||
// new vscode.Range( | ||
// new vscode.Position(parseInt(line), parseInt(char)), | ||
// new vscode.Position(parseInt(line), parseInt(char)) | ||
// ) | ||
// ), | ||
// "hello world" | ||
// ), | ||
// ], | ||
}; | ||
} | ||
import * as vscode from 'vscode'; | ||
import cp = require('child_process'); | ||
import * as util from './util'; | ||
import { goctlOutputChannel } from './extension'; | ||
|
||
export class GoctlDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider { | ||
|
||
coll = vscode.languages.createDiagnosticCollection( | ||
vscode.window.activeTextEditor?.document.fileName | ||
); | ||
|
||
provideDocumentFormattingEdits( | ||
document: vscode.TextDocument, | ||
options: vscode.FormattingOptions, | ||
token: vscode.CancellationToken): vscode.ProviderResult<vscode.TextEdit[]> { | ||
|
||
return this.runFormatter(document, token).then( | ||
(edits) => edits, | ||
(err) => { | ||
if (err) { | ||
let errs = err.split('\n'); | ||
errs.forEach((element: string) => { | ||
if (element.trim().length === 0) { | ||
return; | ||
} | ||
|
||
const errObj = this.createDiagnostic(document, element); | ||
const diagnostics: vscode.Diagnostic[] = []; | ||
|
||
const hasErr = this.coll.get(document.uri); | ||
if (hasErr) { | ||
this.coll.get(document.uri)?.forEach((item) => { | ||
if (item.message === errObj.message) { | ||
return; | ||
} else { | ||
if (item.range.start.line === errObj.range.start.line) { | ||
return; | ||
} | ||
} | ||
}); | ||
} | ||
diagnostics.push(errObj); | ||
this.coll.set(document.uri, diagnostics); | ||
}); | ||
return Promise.reject(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
private runFormatter( | ||
document: vscode.TextDocument, | ||
token: vscode.CancellationToken | ||
): Thenable<vscode.TextEdit[]> { | ||
|
||
const formatFlags = ['api', 'format', '-iu', '-dir', document.fileName, '--stdin']; | ||
return new Promise<vscode.TextEdit[]>((resolve, reject) => { | ||
let stdout = ''; | ||
let stderr = ''; | ||
|
||
const p = cp.spawn("goctl", formatFlags); | ||
|
||
token.onCancellationRequested(() => !p.killed && util.killTree(p.pid)); | ||
p.stdout.setEncoding('utf8'); | ||
p.stdout.on('data', (data) => (stdout += data)); | ||
p.stderr.on('data', (data) => (stderr += data)); | ||
p.on('error', (err) => { | ||
if (!err) { | ||
vscode.window.showWarningMessage('Unknown mistake , please feedback.'); | ||
return reject(); | ||
} | ||
goctlOutputChannel.appendLine(err.toString()); | ||
|
||
let errCode = (<any>err).code; | ||
|
||
switch (errCode) { | ||
case 'ENOENT': { | ||
vscode.window.showInformationMessage("If you don't have goctl installed, you can install it with the following this doc: \"https://github.com/zeromicro/go-zero#6-quick-start\""); | ||
vscode.window.showWarningMessage('Check the console in goctl when formatting. goctl seem not in your $PATH , please try in terminal.'); | ||
break; | ||
} | ||
case 'EACCES': { | ||
vscode.window.showWarningMessage('Check the console in goctl when formatting. goctl seem no executable permissions, please try in terminal.'); | ||
break; | ||
} | ||
default: { | ||
vscode.window.showWarningMessage(err.toString()); | ||
break; | ||
} | ||
} | ||
return reject(); | ||
}); | ||
p.on('close', (code) => { | ||
if (code !== 0) { | ||
return reject(stderr); | ||
} | ||
|
||
// Return the complete file content in the edit. | ||
// VS Code will calculate minimal edits to be applied | ||
const fileStart = new vscode.Position(0, 0); | ||
const fileEnd = document.lineAt(document.lineCount - 1).range.end; | ||
const textEdits: vscode.TextEdit[] = [ | ||
new vscode.TextEdit(new vscode.Range(fileStart, fileEnd), stdout) | ||
]; | ||
|
||
this.coll.delete(document.uri); | ||
|
||
return resolve(textEdits); | ||
}); | ||
if (p.pid) { | ||
p.stdin.end(document.getText()); | ||
} | ||
}); | ||
} | ||
|
||
createDiagnostic(doc: vscode.TextDocument, err: string): vscode.Diagnostic { | ||
let errArr = err.split(' '); | ||
let [line, charStart] = errArr[2].split(':'); | ||
|
||
let source = errArr[0]; | ||
let msg = errArr.splice(3).join(' ').trim(); | ||
|
||
const lineNumber = parseInt(line) - 1; | ||
const text = doc.lineAt(lineNumber); | ||
const charNumber = text.text.trim().length; | ||
const range = new vscode.Range( | ||
new vscode.Position(lineNumber, parseInt(charStart) + 1), | ||
new vscode.Position(lineNumber, charNumber) | ||
); | ||
return { | ||
code: '', | ||
message: msg, | ||
range: range, | ||
severity: vscode.DiagnosticSeverity.Error, | ||
source: source, | ||
}; | ||
} | ||
} |