Skip to content

Commit

Permalink
Support for pasting a java file in the file explorer
Browse files Browse the repository at this point in the history
Signed-off-by: Hope Hadfield <hhadfiel@redhat.com>
  • Loading branch information
hopehadfield committed Nov 29, 2023
1 parent aea6d6f commit 75f5b00
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@
"scope": "machine-overridable",
"order": 20
},
"java.pasteActions.fileExplorer": {
"type": "boolean",
"default": true,
"description": "Enable/disable file explorer paste ability"
},
"java.server.launchMode": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -1487,6 +1492,11 @@
"command": "java.server.restart",
"title": "%java.server.restart%",
"category": "Java"
},
{
"command": "java.action.filesExplorerPasteAction",
"title": "Paste a file",
"category": "Java"
}
],
"keybindings": [
Expand All @@ -1504,6 +1514,12 @@
"key": "ctrl+shift+v",
"mac": "cmd+shift+v",
"when": "javaLSReady && editorLangId == java"
},
{
"command": "java.action.filesExplorerPasteAction",
"key": "ctrl+shift+v",
"mac": "cmd+shift+v",
"when": "(explorerViewletFocus || (workspaceFolderCount == 0 && explorerViewletVisible)) && config.java.pasteActions.fileExplorer"
}
],
"menus": {
Expand Down
9 changes: 9 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export namespace Commands {
* Custom paste action (triggers auto-import)
*/
export const CLIPBOARD_ONPASTE = 'java.action.clipboardPasteAction';
/**
* Custom paste action in files explorer
*/
export const FILESEXPLORER_ONPASTE = 'java.action.filesExplorerPasteAction';
/**
* Choose type to import.
*/
Expand Down Expand Up @@ -341,4 +345,9 @@ export namespace Commands {
*/
export const SMARTSEMICOLON_DETECTION = "java.edit.smartSemicolonDetection";

/**
* Determine if pasted text is a java file and resolve packages
*/
export const RESOLVE_PASTED_TEXT = "java.project.resolveText";

}
9 changes: 9 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib'
import { activationProgressNotification } from "./serverTaskPresenter";
import { loadSupportedJreNames } from './jdkUtils';
import { BuildFileSelector, PICKED_BUILD_FILES, cleanupProjectPickerCache } from './buildFilesSelector';
import { pasteFile } from './pasteAction';

const syntaxClient: SyntaxLanguageClient = new SyntaxLanguageClient();
const standardClient: StandardLanguageClient = new StandardLanguageClient();
Expand Down Expand Up @@ -93,6 +94,14 @@ function getHeapDumpFolderFromSettings(): string {

export async function activate(context: ExtensionContext): Promise<ExtensionAPI> {
await loadSupportedJreNames(context);
context.subscriptions.push(commands.registerCommand(Commands.FILESEXPLORER_ONPASTE, async () => {
const originalClipboard = await env.clipboard.readText();
// Hack in order to get path to selected folder (if applicable)
await commands.executeCommand('copyFilePath');
const folder = await env.clipboard.readText();
await env.clipboard.writeText(originalClipboard);
pasteFile(context, folder);
}));
context.subscriptions.push(markdownPreviewProvider);
context.subscriptions.push(commands.registerCommand(Commands.TEMPLATE_VARIABLES, async () => {
markdownPreviewProvider.show(context.asAbsolutePath(path.join('document', `${Commands.TEMPLATE_VARIABLES}.md`)), 'Predefined Variables', "", context);
Expand Down
37 changes: 36 additions & 1 deletion src/pasteAction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict';

import { commands, env, ExtensionContext, Range, TextEditor, window } from 'vscode';
import { commands, env, ExtensionContext, Range, TextEditor, Uri, window, workspace } from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';

import { Commands } from './commands';

import fs = require('fs');
const os = require('os');
import { apiManager } from './apiManager';
import { TextEncoder } from 'util';

export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand(Commands.CLIPBOARD_ONPASTE, () => {
registerOrganizeImportsOnPasteCommand();
Expand Down Expand Up @@ -48,4 +53,34 @@ export async function registerOrganizeImportsOnPasteCommand(): Promise<void> {
}
}
});
}

let serverReady = false;

export async function pasteFile(context: ExtensionContext, folder: fs.PathLike): Promise<void> {
const clipboardText: string = await env.clipboard.readText();
let filePath = folder.toString();
fs.stat(folder, async (err, stats) => {
// If no project is open in the workspace
if (filePath === "." || workspace.workspaceFolders === undefined) {
filePath = os.homedir();
filePath += "/tmp";
if(!fs.existsSync(filePath)){
fs.mkdirSync(filePath);
}
// If given path to selected folder is invalid (no folder is selected)
} else if (filePath === clipboardText || stats.isFile()) {
filePath = workspace.workspaceFolders[0].uri.path;
}
if (!serverReady) {
await apiManager.getApiInstance().serverReady().then( async () => {
serverReady = true;
});
}
const fileUri = Uri.file(await commands.executeCommand(Commands.RESOLVE_PASTED_TEXT, filePath, clipboardText));
if (fileUri !== null){
await workspace.fs.writeFile(fileUri, new TextEncoder().encode(clipboardText));
window.showTextDocument(fileUri, { preview: false });
}
});
}

0 comments on commit 75f5b00

Please sign in to comment.