11'use strict' ;
22
3- import { commands , env , ExtensionContext , Range , TextEditor , window } from 'vscode' ;
3+ import { TextEncoder } from 'util' ;
4+ import { commands , env , ExtensionContext , Range , TextEditor , Uri , window , workspace } from 'vscode' ;
45import { LanguageClient } from 'vscode-languageclient/node' ;
5-
6+ import { apiManager } from './apiManager' ;
67import { Commands } from './commands' ;
8+ import fs = require( 'fs' ) ;
79
810export function registerCommands ( languageClient : LanguageClient , context : ExtensionContext ) {
911 context . subscriptions . push ( commands . registerCommand ( Commands . CLIPBOARD_ONPASTE , ( ) => {
@@ -48,4 +50,32 @@ export async function registerOrganizeImportsOnPasteCommand(): Promise<void> {
4850 }
4951 }
5052 } ) ;
53+ }
54+
55+ let serverReady = false ;
56+
57+ export async function pasteFile ( folder : fs . PathLike ) : Promise < void > {
58+ const clipboardText : string = await env . clipboard . readText ( ) ;
59+ let filePath = folder . toString ( ) ;
60+ fs . stat ( folder , async ( err , stats ) => {
61+ // If given path to selected folder is invalid (no folder is selected)
62+ if ( filePath === clipboardText || stats . isFile ( ) || ( filePath === "." && workspace . workspaceFolders !== undefined ) ) {
63+ filePath = workspace . workspaceFolders [ 0 ] . uri . fsPath ;
64+ }
65+ if ( ! serverReady ) {
66+ await apiManager . getApiInstance ( ) . serverReady ( ) . then ( async ( ) => {
67+ serverReady = true ;
68+ } ) ;
69+ }
70+ const fileString : string = await commands . executeCommand ( Commands . EXECUTE_WORKSPACE_COMMAND , Commands . RESOLVE_PASTED_TEXT , filePath , clipboardText ) ;
71+ const fileUri = fileString !== null ? Uri . file ( fileString ) : null ;
72+ if ( fileUri !== null ) {
73+ try {
74+ await workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( clipboardText ) ) ;
75+ window . showTextDocument ( fileUri , { preview : false } ) ;
76+ } catch ( error : unknown ) {
77+ // Do nothing (file does not have write permissions)
78+ }
79+ }
80+ } ) ;
5181}
0 commit comments