-
Notifications
You must be signed in to change notification settings - Fork 476
Add missing import on paste #2795
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
Closed
Closed
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
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
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,49 @@ | ||
import { CancellationToken, commands, DataTransfer, DocumentPasteEdit, DocumentPasteEditProvider, DocumentPasteProviderMetadata, DocumentSelector, EndOfLine, ExtensionContext, languages, Range, TextDocument, TextEditor, window, WorkspaceEdit } from "vscode"; | ||
import { LanguageClient } from "vscode-languageclient/node"; | ||
import { Commands } from "./commands"; | ||
import { JAVA_SELECTOR } from "./standardLanguageClient"; | ||
|
||
const TEXT_MIMETYPE: string = "text/plain"; | ||
|
||
const MIMETYPES: DocumentPasteProviderMetadata = { | ||
pasteMimeTypes: [TEXT_MIMETYPE] | ||
}; | ||
|
||
class StringPasteEditProvider implements DocumentPasteEditProvider { | ||
|
||
private client: LanguageClient; | ||
private copiedDocumentUri: string | undefined; | ||
private copiedContent: string | undefined; | ||
|
||
constructor(client: LanguageClient) { | ||
this.client = client; | ||
} | ||
|
||
async prepareDocumentPaste(document: TextDocument, _ranges: readonly Range[], dataTransfer: DataTransfer, _token: CancellationToken): Promise<void> { | ||
const copiedContent: string = await dataTransfer.get(TEXT_MIMETYPE).asString(); | ||
if (copiedContent) { | ||
this.copiedContent = copiedContent; | ||
this.copiedDocumentUri = document.uri.toString(); | ||
} | ||
} | ||
|
||
async provideDocumentPasteEdits(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, _token: CancellationToken): Promise<DocumentPasteEdit> { | ||
const insertContent: string = await dataTransfer.get(TEXT_MIMETYPE).asString(); | ||
if (!insertContent) { | ||
return new DocumentPasteEdit(""); | ||
} | ||
const documentPasteEdit = new DocumentPasteEdit(insertContent); | ||
const workspaceEdit = await commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.ADD_IMPORTS_PASTE, document.uri.toString(), JSON.stringify(this.client.code2ProtocolConverter.asRange(ranges[0])), insertContent, this.copiedContent === insertContent ? this.copiedDocumentUri : null); | ||
if (workspaceEdit) { | ||
documentPasteEdit.additionalEdit = this.client.protocol2CodeConverter.asWorkspaceEdit(workspaceEdit); | ||
} | ||
return documentPasteEdit; | ||
} | ||
|
||
} | ||
|
||
export function registerPasteEventHandler(context: ExtensionContext, client: LanguageClient) { | ||
if (languages["registerDocumentPasteEditProvider"]){ | ||
context.subscriptions.push(languages["registerDocumentPasteEditProvider"](JAVA_SELECTOR, new StringPasteEditProvider(client), MIMETYPES)); | ||
} | ||
} |
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
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,75 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
declare module 'vscode' { | ||
|
||
// https://github.com/microsoft/vscode/issues/30066/ | ||
|
||
/** | ||
* Provider invoked when the user copies and pastes code. | ||
*/ | ||
interface DocumentPasteEditProvider { | ||
|
||
/** | ||
* Optional method invoked after the user copies text in a file. | ||
* | ||
* During {@link prepareDocumentPaste}, an extension can compute metadata that is attached to | ||
* a {@link DataTransfer} and is passed back to the provider in {@link provideDocumentPasteEdits}. | ||
* | ||
* @param document Document where the copy took place. | ||
* @param ranges Ranges being copied in the `document`. | ||
* @param dataTransfer The data transfer associated with the copy. You can store additional values on this for later use in {@link provideDocumentPasteEdits}. | ||
* @param token A cancellation token. | ||
*/ | ||
prepareDocumentPaste?(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): void | Thenable<void>; | ||
|
||
/** | ||
* Invoked before the user pastes into a document. | ||
* | ||
* In this method, extensions can return a workspace edit that replaces the standard pasting behavior. | ||
* | ||
* @param document Document being pasted into | ||
* @param ranges Currently selected ranges in the document. | ||
* @param dataTransfer The data transfer associated with the paste. | ||
* @param token A cancellation token. | ||
* | ||
* @return Optional workspace edit that applies the paste. Return undefined to use standard pasting. | ||
*/ | ||
provideDocumentPasteEdits(document: TextDocument, ranges: readonly Range[], dataTransfer: DataTransfer, token: CancellationToken): ProviderResult<DocumentPasteEdit>; | ||
} | ||
|
||
/** | ||
* An operation applied on paste | ||
*/ | ||
class DocumentPasteEdit { | ||
/** | ||
* The text or snippet to insert at the pasted locations. | ||
*/ | ||
insertText: string | SnippetString; | ||
|
||
/** | ||
* An optional additional edit to apply on paste. | ||
*/ | ||
additionalEdit?: WorkspaceEdit; | ||
|
||
/** | ||
* @param insertText The text or snippet to insert at the pasted locations. | ||
*/ | ||
constructor(insertText: string | SnippetString); | ||
} | ||
|
||
interface DocumentPasteProviderMetadata { | ||
/** | ||
* Mime types that `provideDocumentPasteEdits` should be invoked for. | ||
* | ||
* Use the special `files` mimetype to indicate the provider should be invoked if any files are present in the `DataTransfer`. | ||
*/ | ||
readonly pasteMimeTypes: readonly string[]; | ||
} | ||
|
||
namespace languages { | ||
export function registerDocumentPasteEditProvider(selector: DocumentSelector, provider: DocumentPasteEditProvider, metadata: DocumentPasteProviderMetadata): Disposable; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.