Skip to content
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 for context menus and localization #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var fs = require('fs');
var path = require('path');
var vscode = require('vscode');
var fileService = require('./libs/fileService');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use the existing fileService or modify it if necessary, rather then rewrite a lot of its functionality. The 'Open File...' context menu should just add the option of the UI passing a file location to the fileService to open, in addition to the existing default behaviour of passing the current active document file location to the fileService to open activated by Command/CTRL+ALT+O.

There is another pull request #5 with similar functionality. You may wish to wait for the outcome of that to avoid duplicating efforts.


Expand All @@ -16,57 +18,96 @@ var openController = (function openControllerIIFE() {
var opnOptionObj = config.perLang.opnOptions[i];

if (activeTextEditor.document.languageId === opnOptionObj.forLang) {

fileService.openFileLocation(fileService.getFileLocation(activeTextEditor, config, opnOptionObj), opnOptionObj);
break;

}

}

} else {

fileService.openFileLocation(fileService.getFileLocation(activeTextEditor));
}

};

var openFile = function openUriAnonFn() {

var editor = vscode.window.activeTextEditor;

if (!editor || !editor.document.uri) {
var activeEditor = function activeEditor(editor) {

if (!editor || !editor.document.uri) {
vscode.window.showInformationMessage('No active editor or URI available');
return;

}

openFileWithOptions(editor);
openFileWithOptions(editor);

};

var openFile = function openFile(filePath) {

if (filePath) {

var ext = path.extname(filePath);
var filename = path.basename(filePath, ext);

vscode.window.showTextDocument({
fileName: filename,
uri: vscode.Uri.parse('file:///' + filePath)
}).then(activeEditor);

} else {
var editor = vscode.window.activeTextEditor;
activeEditor(editor);
}
};

var disposable = vscode.commands.registerCommand('extension.opn', function openUriAnonFn() {
var normalizePath = function normalizePath(dir) {

try {
if (process.platform === 'win32') {
dir = dir.replace(/\\/g, '/');
}
return dir;

};

var verifyFileFromPath = function verifyFileFromPath(path) {

openFile();
var dir = fs.statSync(path);
path = normalizePath(path);

if (dir.isFile()) {
return path;
} else {
return null;
}

};

var disposable = vscode.commands.registerCommand('extension.opn', function openUriAnonFn(uri) {

var same = false;
var filePath = null;
var active = vscode.window.activeTextEditor;

if (active) {
same = vscode.window.activeTextEditor.document.uri.fsPath === uri.fsPath;
}

if (uri && !same) {
filePath = verifyFileFromPath(uri.fsPath);
}

try {
openFile(filePath);
}
catch (error) {

vscode.window.showInformationMessage('Error! Could not open file.');
console.error(error.stack);

}

});

var executeOpen = function executeOpenAnonFn() {

var subscriptions = [];
subscriptions.push(disposable);

};

return {
Expand All @@ -76,7 +117,6 @@ var openController = (function openControllerIIFE() {
})();

function activate(context) {

var controller = openController.executeOpen();
context.subscriptions.push(controller);
}
Expand All @@ -88,4 +128,3 @@ function deactivate() {
}

exports.deactivate = deactivate;

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
"title": "Open"
}
],
"menus": {
"explorer/context": [
{
"command": "extension.opn",
"group": "navigation"
}
],
"editor/context": [
{
"command": "extension.opn",
"group": "navigation"
}
]
},
"keybindings": [
{
"command": "extension.opn",
Expand Down