-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial sketches for copy/paste action provider
For #30066
- Loading branch information
Showing
13 changed files
with
370 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
10 changes: 10 additions & 0 deletions
10
src/vs/editor/contrib/copyPasteAction/copyPasteActionContribution.ts
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,10 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions'; | ||
import { CopyPasteActionController } from 'vs/editor/contrib/copyPasteAction/copyPasteActionController'; | ||
|
||
|
||
registerEditorContribution(CopyPasteActionController.ID, CopyPasteActionController); |
118 changes: 118 additions & 0 deletions
118
src/vs/editor/contrib/copyPasteAction/copyPasteActionController.ts
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,118 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { addDisposableListener } from 'vs/base/browser/dom'; | ||
import { CancelablePromise, createCancelablePromise } from 'vs/base/common/async'; | ||
import { Disposable } from 'vs/base/common/lifecycle'; | ||
import { generateUuid } from 'vs/base/common/uuid'; | ||
import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; | ||
import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService'; | ||
import { IEditorContribution } from 'vs/editor/common/editorCommon'; | ||
import { CopyPasteActionProvider, CopyPasteActionProviderRegistry } from 'vs/editor/common/modes'; | ||
|
||
let clipboardItem: undefined | { | ||
readonly handle: string; | ||
readonly results: CancelablePromise<Map<CopyPasteActionProvider, unknown | undefined>>; | ||
}; | ||
|
||
const vscodeClipboardFormat = 'x-vscode/id'; | ||
|
||
export class CopyPasteActionController extends Disposable implements IEditorContribution { | ||
|
||
public static readonly ID = 'editor.contrib.copyPasteActionController'; | ||
|
||
public static get(editor: ICodeEditor): CopyPasteActionController { | ||
return editor.getContribution<CopyPasteActionController>(CopyPasteActionController.ID); | ||
} | ||
|
||
private readonly _editor: ICodeEditor; | ||
|
||
constructor( | ||
editor: ICodeEditor, | ||
@IBulkEditService private readonly _bulkEditService: IBulkEditService, | ||
) { | ||
super(); | ||
|
||
this._editor = editor; | ||
|
||
this._register(addDisposableListener(document, 'copy', (e: ClipboardEvent) => { | ||
if (!e.clipboardData) { | ||
return; | ||
} | ||
|
||
const model = editor.getModel(); | ||
const selection = this._editor.getSelection(); | ||
if (!model || !selection) { | ||
return; | ||
} | ||
|
||
const providers = CopyPasteActionProviderRegistry.all(model).filter(x => !!x.onDidCopy); | ||
if (!providers.length) { | ||
return; | ||
} | ||
|
||
// Call prevent default to prevent our new clipboard data from being overwritten (is this really required?) | ||
e.preventDefault(); | ||
|
||
// And then fill in raw text again since we prevented default | ||
const clipboardText = model.getValueInRange(selection); | ||
e.clipboardData.setData('text/plain', clipboardText); | ||
|
||
// Save off a handle pointing to data that VS Code maintains. | ||
const handle = generateUuid(); | ||
e.clipboardData.setData(vscodeClipboardFormat, handle); | ||
|
||
const promise = createCancelablePromise(async token => { | ||
const results = await Promise.all(providers.map(async provider => { | ||
const result = await provider.onDidCopy!(model, selection, { text: clipboardText }, token); | ||
return { provider, result }; | ||
})); | ||
|
||
const map = new Map<CopyPasteActionProvider, unknown | undefined>(); | ||
for (const { provider, result } of results) { | ||
map.set(provider, result); | ||
} | ||
|
||
return map; | ||
}); | ||
|
||
clipboardItem = { handle: handle, results: promise }; | ||
})); | ||
|
||
this._register(addDisposableListener(document, 'paste', async (e: ClipboardEvent) => { | ||
const model = editor.getModel(); | ||
const selection = this._editor.getSelection(); | ||
if (!model || !selection) { | ||
return; | ||
} | ||
|
||
const providers = CopyPasteActionProviderRegistry.all(model).filter(x => !!x.onDidCopy); | ||
if (!providers.length) { | ||
return; | ||
} | ||
|
||
const handle = e.clipboardData?.getData(vscodeClipboardFormat); | ||
const clipboardText = e.clipboardData?.getData('text/plain') ?? ''; | ||
|
||
e.preventDefault(); | ||
e.stopImmediatePropagation(); | ||
|
||
let results: Map<CopyPasteActionProvider, unknown | undefined> | undefined; | ||
if (handle && clipboardItem && clipboardItem?.handle === handle) { | ||
results = await clipboardItem.results; | ||
} | ||
|
||
for (const provider of providers) { | ||
const data = results?.get(provider); | ||
const edit = await provider.onWillPaste(model, selection, { text: clipboardText, data }); | ||
if (!edit) { | ||
continue; | ||
} | ||
|
||
await this._bulkEditService.apply(ResourceEdit.convert(edit), { editor }); | ||
} | ||
}, true)); | ||
} | ||
} |
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
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
Oops, something went wrong.