refactoring the code by extracting some functionalities out of extension.js

This commit is contained in:
2017-09-25 10:45:27 +08:00
parent 38b1180d2b
commit 542568a037
2 changed files with 103 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
var file = require('./file')
var _ = require('lodash');
var list = [];
var buffer = ""; // to store current file;
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
//add current active tab to list, check if there is an active tab
if (!!vscode.window.activeTextEditor && !!vscode.window.activeTextEditor._documentData) {
buffer = vscode.window.activeTextEditor._documentData._uri.path;
}
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
var disposable = vscode.commands.registerCommand('extension.openRecentFiles', function () {
// The code you place here will be executed every time your command is executed
vscode.window.showQuickPick(file.mapUriToDisplayName(list)).then((item) => {
handleUserInput(item);
});
});
context.subscriptions.push(disposable);
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(handleChangeActiveWindow));
// This line of code will only be executed once when your extension is activated
console.log('"recent-files" is now active!');
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
list = [];
buffer = ''
}
exports.deactivate = deactivate;
function handleUserInput(item) {
if (!item || item.trim === '') return;
let targetUri = _.findLast(list, (uri) => {
return uri.indexOf(item) !== -1;
})
if (!targetUri) {
vscode.window.showWarningMessage("Can't find the file you selected.")
return;
}
let targetDocument = vscode.workspace.openTextDocument(targetUri);
targetDocument.then(function (document) {
vscode.window.showTextDocument(document);
});
}
function handleChangeActiveWindow(e) {
if (!e) return;
//ignore invalid item
if (!e._documentData || !e._documentData._uri) return;
let uri = e._documentData._uri.path
if (!uri || uri.trim() === '') return
//only allow to show 20 files.
if (list.length === 20) list = _.tail(list);
if (file.isFileNameExist(list, uri)) _.remove(list, (item) => {
return item === uri
});
if (!!buffer) list.push(buffer);
buffer = uri; //update buffer
list = _.uniq(list);
}
+25
View File
@@ -0,0 +1,25 @@
var vscode = require('vscode');
var _ = require('lodash');
const mapUriToDisplayName = (list) => {
return _.chain(list.slice()).uniq().reverse().remove(item => {
return item.split(vscode.workspace.rootPath).length != 1;
}).map((item) => {
return item.substring(vscode.workspace.rootPath.length + 1);
}).filter((item) => {
//ignore the system iles which starts with .
return !!item && (item.trim() !== "") && !(item.startsWith("."));
}).value();
}
const isFileNameExist = (list, fileName) => {
return _.some(list, (item) => {
return item === fileName;
})
}
module.exports = {
mapUriToDisplayName,
isFileNameExist
}