-
Notifications
You must be signed in to change notification settings - Fork 441
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
Support refactoring documentation #1334
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
import { CodeActionProvider, CodeActionProviderMetadata, CodeActionKind } from "vscode"; | ||
import { Commands } from "./commands"; | ||
|
||
/** | ||
* Mapping the refactoring kind to its section id in the document | ||
*/ | ||
export const javaRefactorKinds: Map<CodeActionKind, string> = new Map([ | ||
[CodeActionKind.Refactor, 'java-refactoring'], | ||
[CodeActionKind.RefactorExtract, 'extract-to-constant'], | ||
[CodeActionKind.RefactorExtract.append('function'), 'extract-to-method'], | ||
[CodeActionKind.RefactorExtract.append('constant'), 'extract-to-constant'], | ||
[CodeActionKind.RefactorExtract.append('variable'), 'extract-to-local-variable'], | ||
[CodeActionKind.RefactorExtract.append('field'), 'extract-to-field'], | ||
[CodeActionKind.RefactorInline, 'inline-constant'], | ||
[CodeActionKind.Refactor.append('move'), 'move'], | ||
[CodeActionKind.Refactor.append('assign'), 'assign-to-variable'], | ||
[CodeActionKind.Refactor.append('introduce').append('parameter'), 'introduce-parameter'] | ||
]); | ||
|
||
export class RefactorDocumentProvider implements CodeActionProvider { | ||
provideCodeActions() { | ||
return []; | ||
} | ||
|
||
public static readonly metadata: CodeActionProviderMetadata = { | ||
providedCodeActionKinds: [ | ||
CodeActionKind.Refactor | ||
], | ||
documentation: Array.from(javaRefactorKinds.keys()).map(kind => { | ||
return { | ||
kind, | ||
command: { | ||
command: Commands.LEARN_MORE_ABOUT_REFACTORING, | ||
title: 'Learn more about Java refactorings...', | ||
arguments: [kind] | ||
} | ||
}; | ||
}), | ||
}; | ||
} |
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,100 @@ | ||
import { Disposable, WebviewPanel, window, ViewColumn, commands, Uri, Webview, ExtensionContext, env } from "vscode"; | ||
import * as fse from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
class MarkdownPreviewProvider implements Disposable { | ||
private panel: WebviewPanel | undefined; | ||
private disposables: Disposable[] = []; | ||
|
||
public async show(markdownFilePath: string, title: string, section: string, context: ExtensionContext): Promise<void> { | ||
if (!this.panel) { | ||
this.panel = window.createWebviewPanel('java.markdownPreview', title, ViewColumn.Active, { | ||
localResourceRoots: [ | ||
Uri.file(path.join(context.extensionPath, 'webview-resources')), | ||
Uri.file(path.dirname(markdownFilePath)), | ||
], | ||
retainContextWhenHidden: true, | ||
enableFindWidget: true, | ||
enableScripts: true, | ||
}); | ||
} | ||
|
||
this.disposables.push(this.panel.onDidDispose(() => { | ||
this.panel = undefined; | ||
})); | ||
|
||
this.panel.iconPath = Uri.file(path.join(context.extensionPath, 'icons', 'icon128.png')); | ||
this.panel.webview.html = await this.getHtmlContent(this.panel.webview, markdownFilePath, section, context); | ||
|
||
this.panel.reveal(this.panel.viewColumn); | ||
} | ||
|
||
public dispose(): void { | ||
if (this.panel) { | ||
this.panel.dispose(); | ||
} | ||
for (const disposable of this.disposables) { | ||
disposable.dispose(); | ||
} | ||
} | ||
|
||
protected async getHtmlContent(webview: Webview, markdownFilePath: string, section: string, context: ExtensionContext): Promise<string> { | ||
const nonce: string = this.getNonce(); | ||
const styles: string = this.getStyles(webview, context); | ||
let markdownString: string = await fse.readFile(markdownFilePath, 'utf8'); | ||
markdownString = markdownString.replace(/__VSCODE_ENV_APPNAME_PLACEHOLDER__/, env.appName); | ||
const body: string = await commands.executeCommand('markdown.api.render', markdownString); | ||
return ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; img-src 'self' ${webview.cspSource} https: data:; script-src 'nonce-${nonce}';"/> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
${styles} | ||
<base href="${webview.asWebviewUri(Uri.file(markdownFilePath))}"> | ||
</head> | ||
<body class="vscode-body scrollBeyondLastLine wordWrap showEditorSelection"> | ||
${body} | ||
<button class="btn floating-bottom-right" id="back-to-top-btn"> | ||
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"> | ||
<path fill-rule="evenodd" clip-rule="evenodd" d="M8 6.04042L3.02022 11.0202L2.31311 10.3131L7.64644 4.97976L8.35355 4.97976L13.6869 10.3131L12.9798 11.0202L8 6.04042Z"/> | ||
</svg> | ||
</button> | ||
<script nonce="${nonce}"> | ||
(function() { | ||
var element = document.querySelector('[id^="${section}"]'); | ||
if (element) { | ||
element.scrollIntoView(true); | ||
} | ||
var backToTopBtn = document.getElementById('back-to-top-btn'); | ||
if (backToTopBtn) { | ||
backToTopBtn.onclick = () => document.documentElement.scrollTop = 0; | ||
} | ||
})(); | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
} | ||
|
||
protected getStyles(webview: Webview, context: ExtensionContext): string { | ||
const styles: Uri[] = [ | ||
Uri.file(path.join(context.extensionPath, 'webview-resources', 'highlight.css')), | ||
Uri.file(path.join(context.extensionPath, 'webview-resources', 'markdown.css')), | ||
Uri.file(path.join(context.extensionPath, 'webview-resources', 'document.css')), | ||
]; | ||
return styles.map((styleUri: Uri) => `<link rel="stylesheet" type="text/css" href="${webview.asWebviewUri(styleUri).toString()}">`).join('\n'); | ||
} | ||
|
||
private getNonce(): string { | ||
let text = ""; | ||
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
for (let i = 0; i < 32; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
} | ||
} | ||
|
||
export const markdownPreviewProvider: MarkdownPreviewProvider = new MarkdownPreviewProvider(); |
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 @@ | ||
.btn { | ||
border: 0; | ||
color: var(--vscode-button-foreground); | ||
background-color: var(--vscode-button-background); | ||
} | ||
|
||
.btn svg { | ||
fill: var(--vscode-button-foreground); | ||
} | ||
|
||
.btn:hover { | ||
background-color: var(--vscode-button-hoverBackground); | ||
} | ||
|
||
.floating-bottom-right { | ||
position: fixed; | ||
bottom: 1rem; | ||
right: 1rem; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check the new API is available (think Theia/Che) before registering the new provider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any guidance that we could follow to do such checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume something along the lines of
if (CodeActionProviderMetadata !== null)
. But I'm no typescript expertThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeActionProviderMetadata
is only used as a prarm when register the code action provider, so probably it's not able to be used for API checking. Usingenv.appName.includes('VS Code')
?// BTW, will theia/che honor the
engines
field inpackage.json
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
env.appName.includes('VS Code') is bad, since we'd need to rerelease vscode-java once Theia/Che support the feature.
That's a question for @benoitf
I don't understand why we can't do a check on CodeActionProviderMetadata (&& CodeActionProviderMetadata.documentation) like we do for https://github.com/redhat-developer/vscode-java/blob/master/src/semanticTokenProvider.ts#L8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdneo if you link to a vsix build, maybe @benoitf can test it (display the refactoring menu, see there are no errors)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @benoitf,
Would you mind to help test this feature? Here is the vsix link. To test it, you can open a Java file, right click in the editor and click
Learn more about Java refactorings
(if it exists in the context menu).More details can be found in the gif
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is a link to try this vsix with che online: http://che.openshift.io/f/?url=https://gist.githubusercontent.com/benoitf/dcc2fa363b37161f5c8105a8e439a389/raw/55c6c7b117614c45d12a576d8159437c3afd923e/devfile.yaml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
custom documentation does not appear but it's expected as not yet supported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @benoitf!
@fbricon Seems that a RefactorDocumentProvider is registered for nothing.