-
Notifications
You must be signed in to change notification settings - Fork 819
src/goGenerateMethod: make method generation more automatic #2667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sslime336
wants to merge
2
commits into
golang:master
from
sslime336:feat-generate_methods_using_codeaction
+132
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
src/goGenerateMethod.ts
This file contains hidden or 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,119 @@ | ||
import vscode = require('vscode'); | ||
import { CommandFactory } from './commands'; | ||
|
||
const CommandTitle = 'Generate method'; | ||
const Command = 'go.generate.method'; | ||
|
||
export const goGenerateMethod: CommandFactory = () => ( | ||
uncommontypeName: string, | ||
needPtrReceiver: boolean, | ||
endPos: vscode.Position | ||
) => { | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
vscode.window.showErrorMessage('No active editor found.'); | ||
return; | ||
} | ||
const receiverName = getReceiverName(uncommontypeName); | ||
let methodTpl = `\n\nfunc ($\{1:${receiverName}} *${uncommontypeName}) $\{2:methodName}(3ドル) 4ドル {\n\t5ドル\n}0ドル`; | ||
if (!needPtrReceiver) { | ||
methodTpl = `\n\nfunc ($\{1:${receiverName}} ${uncommontypeName}) $\{2:methodName}(3ドル) 4ドル {\n\t5ドル\n}0ドル`; | ||
} | ||
editor.insertSnippet(new vscode.SnippetString(methodTpl), endPos); | ||
}; | ||
|
||
export class MethodGenerationProvider implements vscode.CodeActionProvider { | ||
public static readonly providedCodeActionKinds = [vscode.CodeActionKind.Refactor]; | ||
|
||
async provideCodeActions( | ||
document: vscode.TextDocument, | ||
range: vscode.Range | vscode.Selection, | ||
_context: vscode.CodeActionContext, | ||
_token: vscode.CancellationToken | ||
): Promise<vscode.CodeAction[] | undefined> { | ||
const lineText = document.lineAt(range.start.line).text; | ||
// TODO(sslime336): support the uncommontypes defined in type block. | ||
const uncommontypeName = await this.getUncommontypeName(lineText); | ||
|
||
if (uncommontypeName === '') { | ||
return; | ||
} | ||
|
||
let documentSymbols: vscode.DocumentSymbol[] = []; | ||
await vscode.commands | ||
.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', document.uri) | ||
.then((symbols) => { | ||
documentSymbols = symbols.filter((symbol) => { | ||
const res = symbol.name === uncommontypeName; | ||
return res; | ||
}); | ||
}); | ||
|
||
if (documentSymbols.length === 0) { | ||
return; | ||
} | ||
|
||
const endPos = documentSymbols[0].range.end; | ||
|
||
const genPointerReceiverMethod = new vscode.CodeAction( | ||
'generate method with pointer receiver', | ||
vscode.CodeActionKind.Refactor | ||
); | ||
genPointerReceiverMethod.command = { | ||
title: CommandTitle, | ||
command: Command, | ||
arguments: [uncommontypeName, true, endPos] | ||
}; | ||
|
||
const genValueReceiverMethod = new vscode.CodeAction( | ||
'generate method with value receiver', | ||
vscode.CodeActionKind.Refactor | ||
); | ||
genValueReceiverMethod.command = { | ||
title: CommandTitle, | ||
command: Command, | ||
arguments: [uncommontypeName, false, endPos] | ||
}; | ||
|
||
return [genPointerReceiverMethod, genValueReceiverMethod]; | ||
} | ||
|
||
resolveCodeAction?( | ||
_codeAction: vscode.CodeAction, | ||
_token: vscode.CancellationToken | ||
): vscode.ProviderResult<vscode.CodeAction> { | ||
return; | ||
} | ||
|
||
// getUncommontypeName returns the user defined type's name from type definitions | ||
// that starts with single keyword type. | ||
// The types defined in the type definition block will not satisfied the regexp. | ||
private async getUncommontypeName(lineText: string): Promise<string> { | ||
const regexp = /type ([^0-9]\w+) [^0-9]\w+/; | ||
const matches = lineText.match(regexp); | ||
if (!matches) { | ||
return ''; | ||
} | ||
return matches[1]; | ||
} | ||
} | ||
|
||
// getReceiverName returns the default receiver name which is constructed with uppercase | ||
// characters picked from the structName, it will return the first character in lowercase if | ||
// the structName contains no uppercased character. | ||
function getReceiverName(structName: string): string { | ||
let res = ''; | ||
structName | ||
.split('') | ||
.filter((ch) => isUpperCase(ch)) | ||
.forEach((ch) => (res += ch.toLowerCase())); | ||
if (res === '') { | ||
res = structName.charAt(0); | ||
} | ||
return res; | ||
} | ||
|
||
const isUpperCase = (ch: string): boolean => { | ||
const c = ch.charCodeAt(0); | ||
return 65 <= c && c <= 90; | ||
}; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.