This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add fillstruct tool * Refactoring * add tests and remove config * Fix tests * Use best guess of start line when selection spans multiple lines * Remove unused code * Fix linting errors
- Loading branch information
1 parent
de7f7a8
commit 4884626
Showing
11 changed files
with
234 additions
and
2 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
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
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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
import { byteOffsetAt, getBinPath, getFileArchive, getToolsEnvVars } from './util'; | ||
import cp = require('child_process'); | ||
import { promptForMissingTool } from './goInstallTools'; | ||
|
||
// Interface for the output from fillstruct | ||
interface GoFillStructOutput { | ||
start: number; | ||
end: number; | ||
code: string; | ||
} | ||
|
||
export function runFillStruct(editor: vscode.TextEditor) { | ||
let args = getCommonArgs(editor); | ||
if (!args) { | ||
return; | ||
} | ||
|
||
return execFillStruct(editor, args); | ||
} | ||
|
||
function getCommonArgs(editor: vscode.TextEditor): string[] { | ||
if (!editor) { | ||
vscode.window.showInformationMessage('No editor is active.'); | ||
return; | ||
} | ||
if (!editor.document.fileName.endsWith('.go')) { | ||
vscode.window.showInformationMessage('Current file is not a Go file.'); | ||
return; | ||
} | ||
let args = ['-modified', '-file', editor.document.fileName]; | ||
if (editor.selection.isEmpty) { | ||
let offset = byteOffsetAt(editor.document, editor.selection.start); | ||
args.push('-offset'); | ||
args.push(offset.toString()); | ||
} else { | ||
args.push('-line'); | ||
args.push(`${editor.selection.start.line + 1}`); | ||
} | ||
return args; | ||
} | ||
|
||
function getTabsCount(editor: vscode.TextEditor): number { | ||
let startline = editor.selection.start.line; | ||
let tabs = editor.document.lineAt(startline).text.match('^\t*'); | ||
return tabs.length; | ||
} | ||
|
||
function execFillStruct(editor: vscode.TextEditor, args: string[]): Promise<void> { | ||
let fillstruct = getBinPath('fillstruct'); | ||
let input = getFileArchive(editor.document); | ||
let tabsCount = getTabsCount(editor); | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
let p = cp.execFile(fillstruct, args, { env: getToolsEnvVars() }, (err, stdout, stderr) => { | ||
try { | ||
if (err && (<any>err).code === 'ENOENT') { | ||
promptForMissingTool('fillstruct'); | ||
return reject(); | ||
} | ||
if (err) { | ||
vscode.window.showInformationMessage(`Cannot fill struct: ${stderr}`); | ||
return reject(); | ||
} | ||
|
||
let output = <GoFillStructOutput[]>JSON.parse(stdout); | ||
|
||
if (output.length === 0) { | ||
vscode.window.showInformationMessage(`Got empty fillstruct output`); | ||
return reject(); | ||
} | ||
|
||
let indent = '\t'.repeat(tabsCount); | ||
|
||
editor.edit(editBuilder => { | ||
output.forEach((structToFill) => { | ||
const out = structToFill.code.replace(/\n/g, '\n' + indent); | ||
const rangeToReplace = new vscode.Range(editor.document.positionAt(structToFill.start), | ||
editor.document.positionAt(structToFill.end)); | ||
editBuilder.replace(rangeToReplace, out); | ||
}); | ||
}).then(() => resolve()); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
p.stdin.end(input); | ||
}); | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
type Struct struct { | ||
String string | ||
Number int | ||
Float float64 | ||
Time time.Time | ||
} | ||
|
||
func main() { | ||
myStruct := Struct{ | ||
String: "", | ||
Number: 0, | ||
Float: 0.0, | ||
Time: time.Time{}, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
_ = http.Client{ | ||
Transport: nil, | ||
CheckRedirect: nil, | ||
Jar: nil, | ||
Timeout: 0, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
type Struct struct { | ||
String string | ||
Number int | ||
Float float64 | ||
Time time.Time | ||
} | ||
|
||
func main() { | ||
myStruct := Struct{} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
_ = http.Client{} | ||
} |
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