@gedit/command
Advanced tools
Comparing version
@@ -29,80 +29,8 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CommandRegistry = exports.CommandService = exports.commandServicePath = exports.CommandContribution = exports.Command = void 0; | ||
var inversify_1 = require("inversify"); | ||
var utils_1 = require("@gedit/utils"); | ||
var utils_2 = require("@gedit/utils"); | ||
var utils_3 = require("@gedit/utils"); | ||
const inversify_1 = require("inversify"); | ||
const utils_1 = require("@gedit/utils"); | ||
const utils_2 = require("@gedit/utils"); | ||
const utils_3 = require("@gedit/utils"); | ||
var Command; | ||
@@ -119,4 +47,4 @@ (function (Command) { | ||
if (a.label && b.label) { | ||
var aCommand = (a.category ? "".concat(a.category, ": ").concat(a.label) : a.label).toLowerCase(); | ||
var bCommand = (b.category ? "".concat(b.category, ": ").concat(b.label) : b.label).toLowerCase(); | ||
const aCommand = (a.category ? `${a.category}: ${a.label}` : a.label).toLowerCase(); | ||
const bCommand = (b.category ? `${b.category}: ${b.label}` : b.label).toLowerCase(); | ||
return (aCommand).localeCompare(bCommand); | ||
@@ -149,4 +77,4 @@ } | ||
*/ | ||
var CommandRegistry = /** @class */ (function () { | ||
function CommandRegistry(contributionProvider) { | ||
let CommandRegistry = class CommandRegistry { | ||
constructor(contributionProvider) { | ||
this.contributionProvider = contributionProvider; | ||
@@ -163,74 +91,40 @@ this._commands = {}; | ||
} | ||
Object.defineProperty(CommandRegistry.prototype, "recent", { | ||
/** | ||
* Get the list of recently used commands. | ||
*/ | ||
get: function () { | ||
return this._recent; | ||
}, | ||
/** | ||
* Set the list of recently used commands. | ||
* @param commands the list of recently used commands. | ||
*/ | ||
set: function (commands) { | ||
this._recent = commands; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(CommandRegistry.prototype, "commands", { | ||
/** | ||
* Get all registered commands. | ||
*/ | ||
get: function () { | ||
var e_1, _a; | ||
var commands = []; | ||
try { | ||
for (var _b = __values(this.commandIds), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var id = _c.value; | ||
var cmd = this.getCommand(id); | ||
if (cmd) { | ||
commands.push(cmd); | ||
} | ||
} | ||
/** | ||
* Get the list of recently used commands. | ||
*/ | ||
get recent() { | ||
return this._recent; | ||
} | ||
/** | ||
* Set the list of recently used commands. | ||
* @param commands the list of recently used commands. | ||
*/ | ||
set recent(commands) { | ||
this._recent = commands; | ||
} | ||
/** | ||
* Get all registered commands. | ||
*/ | ||
get commands() { | ||
const commands = []; | ||
for (const id of this.commandIds) { | ||
const cmd = this.getCommand(id); | ||
if (cmd) { | ||
commands.push(cmd); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return commands; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(CommandRegistry.prototype, "commandIds", { | ||
/** | ||
* Get all registered commands identifiers. | ||
*/ | ||
get: function () { | ||
return Object.keys(this._commands); | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
CommandRegistry.prototype.onStart = function () { | ||
var e_2, _a; | ||
var contributions = this.contributionProvider.getContributions(); | ||
try { | ||
for (var contributions_1 = __values(contributions), contributions_1_1 = contributions_1.next(); !contributions_1_1.done; contributions_1_1 = contributions_1.next()) { | ||
var contrib = contributions_1_1.value; | ||
contrib.registerCommands(this); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (contributions_1_1 && !contributions_1_1.done && (_a = contributions_1.return)) _a.call(contributions_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
return commands; | ||
} | ||
/** | ||
* Get all registered commands identifiers. | ||
*/ | ||
get commandIds() { | ||
return Object.keys(this._commands); | ||
} | ||
onStart() { | ||
const contributions = this.contributionProvider.getContributions(); | ||
for (const contrib of contributions) { | ||
contrib.registerCommands(this); | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -241,9 +135,8 @@ * Register the given command and handler if present. | ||
*/ | ||
CommandRegistry.prototype.registerCommand = function (command, handler) { | ||
var _this = this; | ||
registerCommand(command, handler) { | ||
if (this._commands[command.id]) { | ||
console.warn("A command ".concat(command.id, " is already registered.")); | ||
console.warn(`A command ${command.id} is already registered.`); | ||
return utils_2.Disposable.NULL; | ||
} | ||
var toDispose = new utils_2.DisposableCollection(this.doRegisterCommand(command)); | ||
const toDispose = new utils_2.DisposableCollection(this.doRegisterCommand(command)); | ||
if (handler) { | ||
@@ -253,12 +146,12 @@ toDispose.push(this.registerHandler(command.id, handler)); | ||
this.toUnregisterCommands.set(command.id, toDispose); | ||
toDispose.push(utils_2.Disposable.create(function () { return _this.toUnregisterCommands.delete(command.id); })); | ||
toDispose.push(utils_2.Disposable.create(() => this.toUnregisterCommands.delete(command.id))); | ||
return toDispose; | ||
}; | ||
CommandRegistry.prototype.unregisterCommand = function (commandOrId) { | ||
var id = Command.is(commandOrId) ? commandOrId.id : commandOrId; | ||
var toUnregister = this.toUnregisterCommands.get(id); | ||
} | ||
unregisterCommand(commandOrId) { | ||
const id = Command.is(commandOrId) ? commandOrId.id : commandOrId; | ||
const toUnregister = this.toUnregisterCommands.get(id); | ||
if (toUnregister) { | ||
toUnregister.dispose(); | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -271,4 +164,4 @@ * Register the given handler for the given command identifier. | ||
*/ | ||
CommandRegistry.prototype.registerHandler = function (commandId, handler) { | ||
var handlers = this._handlers[commandId]; | ||
registerHandler(commandId, handler) { | ||
let handlers = this._handlers[commandId]; | ||
if (!handlers) { | ||
@@ -279,4 +172,4 @@ this._handlers[commandId] = handlers = []; | ||
return { | ||
dispose: function () { | ||
var idx = handlers.indexOf(handler); | ||
dispose: () => { | ||
const idx = handlers.indexOf(handler); | ||
if (idx >= 0) { | ||
@@ -287,3 +180,3 @@ handlers.splice(idx, 1); | ||
}; | ||
}; | ||
} | ||
/** | ||
@@ -293,9 +186,5 @@ * Test whether there is an active handler for the given command. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.isEnabled = function (command) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return typeof this.getActiveHandler.apply(this, __spreadArray([command], __read(args), false)) !== 'undefined'; | ||
}; | ||
isEnabled(command, ...args) { | ||
return typeof this.getActiveHandler(command, ...args) !== 'undefined'; | ||
} | ||
/** | ||
@@ -305,9 +194,5 @@ * Test whether there is a visible handler for the given command. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.isVisible = function (command) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return typeof this.getVisibleHandler.apply(this, __spreadArray([command], __read(args), false)) !== 'undefined'; | ||
}; | ||
isVisible(command, ...args) { | ||
return typeof this.getVisibleHandler(command, ...args) !== 'undefined'; | ||
} | ||
/** | ||
@@ -317,9 +202,5 @@ * Test whether there is a toggled handler for the given command. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.isToggled = function (command) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return typeof this.getToggledHandler.apply(this, __spreadArray([command], __read(args), false)) !== 'undefined'; | ||
}; | ||
isToggled(command, ...args) { | ||
return typeof this.getToggledHandler(command, ...args) !== 'undefined'; | ||
} | ||
/** | ||
@@ -331,36 +212,20 @@ * Execute the active handler for the given command and arguments. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.executeCommand = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
async executeCommand(commandId, ...args) { | ||
const handler = this.getActiveHandler(commandId, ...args); | ||
if (handler) { | ||
await this.fireWillExecuteCommand(commandId, args); | ||
const result = await handler.execute(...args); | ||
this.onDidExecuteCommandEmitter.fire({ commandId, args }); | ||
return result; | ||
} | ||
return __awaiter(this, void 0, void 0, function () { | ||
var handler, result, argsMessage; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
handler = this.getActiveHandler.apply(this, __spreadArray([commandId], __read(args), false)); | ||
if (!handler) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.fireWillExecuteCommand(commandId, args)]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, handler.execute.apply(handler, __spreadArray([], __read(args), false))]; | ||
case 2: | ||
result = _a.sent(); | ||
this.onDidExecuteCommandEmitter.fire({ commandId: commandId, args: args }); | ||
return [2 /*return*/, result]; | ||
case 3: | ||
argsMessage = ''; | ||
try { | ||
argsMessage = args && args.length > 0 ? " (args: ".concat(JSON.stringify(args), ")") : ''; | ||
} | ||
catch (e) { | ||
argsMessage = args && args.length > 0 ? " (args: ".concat(args, ")") : ''; | ||
} | ||
// eslint-disable-next-line max-len | ||
throw Object.assign(new Error("The command '".concat(commandId, "' cannot be executed. There are no active handlers available for the command.").concat(argsMessage)), { code: 'NO_ACTIVE_HANDLER' }); | ||
} | ||
}); | ||
}); | ||
}; | ||
let argsMessage = ''; | ||
try { | ||
argsMessage = args && args.length > 0 ? ` (args: ${JSON.stringify(args)})` : ''; | ||
} | ||
catch (e) { | ||
argsMessage = args && args.length > 0 ? ` (args: ${args})` : ''; | ||
} | ||
// eslint-disable-next-line max-len | ||
throw Object.assign(new Error(`The command '${commandId}' cannot be executed. There are no active handlers available for the command.${argsMessage}`), { code: 'NO_ACTIVE_HANDLER' }); | ||
} | ||
/** | ||
@@ -370,33 +235,18 @@ * Get a visible handler for the given command or `undefined`. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.getVisibleHandler = function (commandId) { | ||
var e_3, _a; | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handlers = this._handlers[commandId]; | ||
getVisibleHandler(commandId, ...args) { | ||
const handlers = this._handlers[commandId]; | ||
if (handlers) { | ||
try { | ||
for (var handlers_1 = __values(handlers), handlers_1_1 = handlers_1.next(); !handlers_1_1.done; handlers_1_1 = handlers_1.next()) { | ||
var handler = handlers_1_1.value; | ||
try { | ||
if (!handler.isVisible || handler.isVisible.apply(handler, __spreadArray([], __read(args), false))) { | ||
return handler; | ||
} | ||
for (const handler of handlers) { | ||
try { | ||
if (!handler.isVisible || handler.isVisible(...args)) { | ||
return handler; | ||
} | ||
catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (handlers_1_1 && !handlers_1_1.done && (_a = handlers_1.return)) _a.call(handlers_1); | ||
catch (error) { | ||
console.error(error); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
return undefined; | ||
}; | ||
} | ||
/** | ||
@@ -406,33 +256,18 @@ * Get an active handler for the given command or `undefined`. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.getActiveHandler = function (commandId) { | ||
var e_4, _a; | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handlers = this._handlers[commandId]; | ||
getActiveHandler(commandId, ...args) { | ||
const handlers = this._handlers[commandId]; | ||
if (handlers) { | ||
try { | ||
for (var handlers_2 = __values(handlers), handlers_2_1 = handlers_2.next(); !handlers_2_1.done; handlers_2_1 = handlers_2.next()) { | ||
var handler = handlers_2_1.value; | ||
try { | ||
if (!handler.isEnabled || handler.isEnabled.apply(handler, __spreadArray([], __read(args), false))) { | ||
return handler; | ||
} | ||
for (const handler of handlers) { | ||
try { | ||
if (!handler.isEnabled || handler.isEnabled(...args)) { | ||
return handler; | ||
} | ||
catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} | ||
catch (e_4_1) { e_4 = { error: e_4_1 }; } | ||
finally { | ||
try { | ||
if (handlers_2_1 && !handlers_2_1.done && (_a = handlers_2.return)) _a.call(handlers_2); | ||
catch (error) { | ||
console.error(error); | ||
} | ||
finally { if (e_4) throw e_4.error; } | ||
} | ||
} | ||
return undefined; | ||
}; | ||
} | ||
/** | ||
@@ -442,33 +277,18 @@ * Get a toggled handler for the given command or `undefined`. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.getToggledHandler = function (commandId) { | ||
var e_5, _a; | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handlers = this._handlers[commandId]; | ||
getToggledHandler(commandId, ...args) { | ||
const handlers = this._handlers[commandId]; | ||
if (handlers) { | ||
try { | ||
for (var handlers_3 = __values(handlers), handlers_3_1 = handlers_3.next(); !handlers_3_1.done; handlers_3_1 = handlers_3.next()) { | ||
var handler = handlers_3_1.value; | ||
try { | ||
if (handler.isToggled && handler.isToggled.apply(handler, __spreadArray([], __read(args), false))) { | ||
return handler; | ||
} | ||
for (const handler of handlers) { | ||
try { | ||
if (handler.isToggled && handler.isToggled(...args)) { | ||
return handler; | ||
} | ||
catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} | ||
catch (e_5_1) { e_5 = { error: e_5_1 }; } | ||
finally { | ||
try { | ||
if (handlers_3_1 && !handlers_3_1.done && (_a = handlers_3.return)) _a.call(handlers_3); | ||
catch (error) { | ||
console.error(error); | ||
} | ||
finally { if (e_5) throw e_5.error; } | ||
} | ||
} | ||
return undefined; | ||
}; | ||
} | ||
/** | ||
@@ -478,12 +298,12 @@ * Returns with all handlers for the given command. If the command does not have any handlers, | ||
*/ | ||
CommandRegistry.prototype.getAllHandlers = function (commandId) { | ||
var handlers = this._handlers[commandId]; | ||
getAllHandlers(commandId) { | ||
const handlers = this._handlers[commandId]; | ||
return handlers ? handlers.slice() : []; | ||
}; | ||
} | ||
/** | ||
* Get a command for the given command identifier. | ||
*/ | ||
CommandRegistry.prototype.getCommand = function (id) { | ||
getCommand(id) { | ||
return this._commands[id]; | ||
}; | ||
} | ||
/** | ||
@@ -495,10 +315,9 @@ * Adds a command to recently used list. | ||
*/ | ||
CommandRegistry.prototype.addRecentCommand = function (recent) { | ||
var _this = this; | ||
addRecentCommand(recent) { | ||
if (Array.isArray(recent)) { | ||
recent.forEach(function (command) { return _this.addRecentCommand(command); }); | ||
recent.forEach((command) => this.addRecentCommand(command)); | ||
} | ||
else { | ||
// Determine if the command currently exists in the recently used list. | ||
var index = this._recent.findIndex(function (command) { return Command.equals(recent, command); }); | ||
const index = this._recent.findIndex((command) => Command.equals(recent, command)); | ||
// If the command exists, remove it from the array so it can later be placed at the top. | ||
@@ -511,41 +330,29 @@ if (index >= 0) { | ||
} | ||
}; | ||
} | ||
/** | ||
* Clear the list of recently used commands. | ||
*/ | ||
CommandRegistry.prototype.clearCommandHistory = function () { | ||
clearCommandHistory() { | ||
this.recent = []; | ||
}; | ||
CommandRegistry.prototype.doRegisterCommand = function (command) { | ||
var _this = this; | ||
} | ||
doRegisterCommand(command) { | ||
this._commands[command.id] = command; | ||
return { | ||
dispose: function () { | ||
delete _this._commands[command.id]; | ||
dispose: () => { | ||
delete this._commands[command.id]; | ||
} | ||
}; | ||
}; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
CommandRegistry.prototype.fireWillExecuteCommand = function (commandId, args) { | ||
if (args === void 0) { args = []; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, utils_1.WaitUntilEvent.fire(this.onWillExecuteCommandEmitter, { commandId: commandId, args: args }, 30000)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
CommandRegistry = __decorate([ | ||
(0, inversify_1.injectable)(), | ||
__param(0, (0, inversify_1.inject)(utils_3.ContributionProvider)), | ||
__param(0, (0, inversify_1.named)(exports.CommandContribution)), | ||
__metadata("design:paramtypes", [Object]) | ||
], CommandRegistry); | ||
return CommandRegistry; | ||
}()); | ||
async fireWillExecuteCommand(commandId, args = []) { | ||
await utils_1.WaitUntilEvent.fire(this.onWillExecuteCommandEmitter, { commandId, args }, 30000); | ||
} | ||
}; | ||
CommandRegistry = __decorate([ | ||
(0, inversify_1.injectable)(), | ||
__param(0, (0, inversify_1.inject)(utils_3.ContributionProvider)), | ||
__param(0, (0, inversify_1.named)(exports.CommandContribution)), | ||
__metadata("design:paramtypes", [Object]) | ||
], CommandRegistry); | ||
exports.CommandRegistry = CommandRegistry; | ||
//# sourceMappingURL=command.js.map |
@@ -17,169 +17,95 @@ "use strict"; | ||
********************************************************************************/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var command_1 = require("./command"); | ||
var chai = require("chai"); | ||
var expect = chai.expect; | ||
var commandRegistry; | ||
const command_1 = require("./command"); | ||
const chai = require("chai"); | ||
const expect = chai.expect; | ||
let commandRegistry; | ||
/* eslint-disable no-unused-expressions */ | ||
describe('Commands', function () { | ||
beforeEach(function () { | ||
describe('Commands', () => { | ||
beforeEach(() => { | ||
commandRegistry = new command_1.CommandRegistry(new EmptyContributionProvider()); | ||
}); | ||
it('should register and execute a given command', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var concatId, command, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
concatId = 'concat'; | ||
command = { id: concatId }; | ||
commandRegistry.registerCommand(command, new ConcatCommandHandler()); | ||
return [4 /*yield*/, commandRegistry.executeCommand(concatId, 'a', 'b', 'c')]; | ||
case 1: | ||
result = _a.sent(); | ||
expect('abc').equals(result); | ||
return [2 /*return*/]; | ||
} | ||
it('should register and execute a given command', async () => { | ||
const concatId = 'concat'; | ||
const command = { id: concatId }; | ||
commandRegistry.registerCommand(command, new ConcatCommandHandler()); | ||
const result = await commandRegistry.executeCommand(concatId, 'a', 'b', 'c'); | ||
expect('abc').equals(result); | ||
}); | ||
it('should add command to recently used', async () => { | ||
const commandId = 'stub'; | ||
const command = { id: commandId }; | ||
commandRegistry.registerCommand(command, new StubCommandHandler()); | ||
commandRegistry.addRecentCommand(command); | ||
expect(commandRegistry.recent.length).equal(1); | ||
}); | ||
it('should add multiple commands to recently used in the order they were used', async () => { | ||
const commandIds = ['a', 'b', 'c']; | ||
const commands = [ | ||
{ id: commandIds[0] }, | ||
{ id: commandIds[1] }, | ||
{ id: commandIds[2] }, | ||
]; | ||
// Register each command. | ||
commands.forEach((c) => { | ||
commandRegistry.registerCommand(c, new StubCommandHandler()); | ||
}); | ||
}); }); | ||
it('should add command to recently used', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var commandId, command; | ||
return __generator(this, function (_a) { | ||
commandId = 'stub'; | ||
command = { id: commandId }; | ||
commandRegistry.registerCommand(command, new StubCommandHandler()); | ||
commandRegistry.addRecentCommand(command); | ||
expect(commandRegistry.recent.length).equal(1); | ||
return [2 /*return*/]; | ||
// Execute order c, b, a. | ||
commandRegistry.addRecentCommand(commands[2]); | ||
commandRegistry.addRecentCommand(commands[1]); | ||
commandRegistry.addRecentCommand(commands[0]); | ||
// Expect recently used to be a, b, c. | ||
const result = commandRegistry.recent; | ||
expect(result.length).equal(3); | ||
expect(result[0].id).equal(commandIds[0]); | ||
expect(result[1].id).equal(commandIds[1]); | ||
expect(result[2].id).equal(commandIds[2]); | ||
}); | ||
it('should add a previously used command to the top of the most recently used', async () => { | ||
const commandIds = ['a', 'b', 'c']; | ||
const commands = [ | ||
{ id: commandIds[0] }, | ||
{ id: commandIds[1] }, | ||
{ id: commandIds[2] }, | ||
]; | ||
// Register each command. | ||
commands.forEach((c) => { | ||
commandRegistry.registerCommand(c, new StubCommandHandler()); | ||
}); | ||
}); }); | ||
it('should add multiple commands to recently used in the order they were used', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var commandIds, commands, result; | ||
return __generator(this, function (_a) { | ||
commandIds = ['a', 'b', 'c']; | ||
commands = [ | ||
{ id: commandIds[0] }, | ||
{ id: commandIds[1] }, | ||
{ id: commandIds[2] }, | ||
]; | ||
// Register each command. | ||
commands.forEach(function (c) { | ||
commandRegistry.registerCommand(c, new StubCommandHandler()); | ||
}); | ||
// Execute order c, b, a. | ||
commandRegistry.addRecentCommand(commands[2]); | ||
commandRegistry.addRecentCommand(commands[1]); | ||
commandRegistry.addRecentCommand(commands[0]); | ||
result = commandRegistry.recent; | ||
expect(result.length).equal(3); | ||
expect(result[0].id).equal(commandIds[0]); | ||
expect(result[1].id).equal(commandIds[1]); | ||
expect(result[2].id).equal(commandIds[2]); | ||
return [2 /*return*/]; | ||
// Execute order a, b, c, a. | ||
commandRegistry.addRecentCommand(commands[0]); | ||
commandRegistry.addRecentCommand(commands[1]); | ||
commandRegistry.addRecentCommand(commands[2]); | ||
commandRegistry.addRecentCommand(commands[0]); | ||
// Expect recently used to be a, b, c. | ||
const result = commandRegistry.recent; | ||
expect(result.length).equal(3); | ||
expect(result[0].id).equal(commandIds[0]); | ||
expect(result[1].id).equal(commandIds[2]); | ||
expect(result[2].id).equal(commandIds[1]); | ||
}); | ||
it('should clear the recently used command history', async () => { | ||
const commandIds = ['a', 'b', 'c']; | ||
const commands = [ | ||
{ id: commandIds[0] }, | ||
{ id: commandIds[1] }, | ||
{ id: commandIds[2] }, | ||
]; | ||
// Register each command. | ||
commands.forEach((c) => { | ||
commandRegistry.registerCommand(c, new StubCommandHandler()); | ||
}); | ||
}); }); | ||
it('should add a previously used command to the top of the most recently used', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var commandIds, commands, result; | ||
return __generator(this, function (_a) { | ||
commandIds = ['a', 'b', 'c']; | ||
commands = [ | ||
{ id: commandIds[0] }, | ||
{ id: commandIds[1] }, | ||
{ id: commandIds[2] }, | ||
]; | ||
// Register each command. | ||
commands.forEach(function (c) { | ||
commandRegistry.registerCommand(c, new StubCommandHandler()); | ||
}); | ||
// Execute order a, b, c, a. | ||
commandRegistry.addRecentCommand(commands[0]); | ||
commandRegistry.addRecentCommand(commands[1]); | ||
commandRegistry.addRecentCommand(commands[2]); | ||
commandRegistry.addRecentCommand(commands[0]); | ||
result = commandRegistry.recent; | ||
expect(result.length).equal(3); | ||
expect(result[0].id).equal(commandIds[0]); | ||
expect(result[1].id).equal(commandIds[2]); | ||
expect(result[2].id).equal(commandIds[1]); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
it('should clear the recently used command history', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var commandIds, commands; | ||
return __generator(this, function (_a) { | ||
commandIds = ['a', 'b', 'c']; | ||
commands = [ | ||
{ id: commandIds[0] }, | ||
{ id: commandIds[1] }, | ||
{ id: commandIds[2] }, | ||
]; | ||
// Register each command. | ||
commands.forEach(function (c) { | ||
commandRegistry.registerCommand(c, new StubCommandHandler()); | ||
}); | ||
// Execute each command. | ||
commandRegistry.addRecentCommand(commands[0]); | ||
commandRegistry.addRecentCommand(commands[1]); | ||
commandRegistry.addRecentCommand(commands[2]); | ||
// Clear the list of recently used commands. | ||
commandRegistry.clearCommandHistory(); | ||
expect(commandRegistry.recent.length).equal(0); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
it('should return with an empty array of handlers if the command is not registered', function () { | ||
// Execute each command. | ||
commandRegistry.addRecentCommand(commands[0]); | ||
commandRegistry.addRecentCommand(commands[1]); | ||
commandRegistry.addRecentCommand(commands[2]); | ||
// Clear the list of recently used commands. | ||
commandRegistry.clearCommandHistory(); | ||
expect(commandRegistry.recent.length).equal(0); | ||
}); | ||
it('should return with an empty array of handlers if the command is not registered', () => { | ||
expect(commandRegistry.getCommand('missing')).to.be.undefined; | ||
expect(commandRegistry.getAllHandlers('missing')).to.be.empty; | ||
}); | ||
it('should return with an empty array of handlers if the command has no registered handlers', function () { | ||
it('should return with an empty array of handlers if the command has no registered handlers', () => { | ||
commandRegistry.registerCommand({ id: 'id' }); | ||
@@ -189,3 +115,3 @@ expect(commandRegistry.getCommand('id')).to.be.not.undefined; | ||
}); | ||
it('should return all handlers including the non active ones', function () { | ||
it('should return all handlers including the non active ones', () => { | ||
commandRegistry.registerCommand({ id: 'id' }); | ||
@@ -196,33 +122,33 @@ commandRegistry.registerHandler('id', new StubCommandHandler()); | ||
}); | ||
describe('compareCommands', function () { | ||
it('should sort command \'a\' before command \'b\' with categories', function () { | ||
var a = { id: 'a', category: 'a', label: 'a' }; | ||
var b = { id: 'b', category: 'b', label: 'b' }; | ||
describe('compareCommands', () => { | ||
it('should sort command \'a\' before command \'b\' with categories', () => { | ||
const a = { id: 'a', category: 'a', label: 'a' }; | ||
const b = { id: 'b', category: 'b', label: 'b' }; | ||
expect(command_1.Command.compareCommands(a, b)).to.equal(-1); | ||
expect(command_1.Command.compareCommands(b, a)).to.equal(1); | ||
}); | ||
it('should sort command \'a\' before command \'b\' without categories', function () { | ||
var a = { id: 'a', label: 'a' }; | ||
var b = { id: 'b', label: 'b' }; | ||
it('should sort command \'a\' before command \'b\' without categories', () => { | ||
const a = { id: 'a', label: 'a' }; | ||
const b = { id: 'b', label: 'b' }; | ||
expect(command_1.Command.compareCommands(a, b)).to.equal(-1); | ||
expect(command_1.Command.compareCommands(b, a)).to.equal(1); | ||
}); | ||
it('should sort command \'a\' before command \'b\' with mix-match categories', function () { | ||
var a = { id: 'a', category: 'a', label: 'a' }; | ||
var b = { id: 'b', label: 'a' }; | ||
it('should sort command \'a\' before command \'b\' with mix-match categories', () => { | ||
const a = { id: 'a', category: 'a', label: 'a' }; | ||
const b = { id: 'b', label: 'a' }; | ||
expect(command_1.Command.compareCommands(a, b)).to.equal(1); | ||
expect(command_1.Command.compareCommands(b, a)).to.equal(-1); | ||
}); | ||
it('should sort irregardless of casing', function () { | ||
var lowercase = { id: 'a', label: 'a' }; | ||
var uppercase = { id: 'a', label: 'A' }; | ||
it('should sort irregardless of casing', () => { | ||
const lowercase = { id: 'a', label: 'a' }; | ||
const uppercase = { id: 'a', label: 'A' }; | ||
expect(command_1.Command.compareCommands(lowercase, uppercase)).to.equal(0); | ||
}); | ||
it('should not sort if commands are equal', function () { | ||
var a = { id: 'a', label: 'a' }; | ||
it('should not sort if commands are equal', () => { | ||
const a = { id: 'a', label: 'a' }; | ||
expect(command_1.Command.compareCommands(a, a)).to.equal(0); | ||
}); | ||
it('should not sort commands without labels', function () { | ||
var a = { id: 'a' }; | ||
var b = { id: 'b' }; | ||
it('should not sort commands without labels', () => { | ||
const a = { id: 'a' }; | ||
const b = { id: 'b' }; | ||
expect(command_1.Command.compareCommands(a, b)).to.equal(0); | ||
@@ -232,48 +158,26 @@ }); | ||
}); | ||
var EmptyContributionProvider = /** @class */ (function () { | ||
function EmptyContributionProvider() { | ||
} | ||
EmptyContributionProvider.prototype.getContributions = function (recursive) { | ||
class EmptyContributionProvider { | ||
getContributions(recursive) { | ||
return []; | ||
}; | ||
return EmptyContributionProvider; | ||
}()); | ||
var ConcatCommandHandler = /** @class */ (function () { | ||
function ConcatCommandHandler() { | ||
} | ||
ConcatCommandHandler.prototype.execute = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var concat = ''; | ||
args.forEach(function (element) { | ||
} | ||
class ConcatCommandHandler { | ||
execute(...args) { | ||
let concat = ''; | ||
args.forEach(element => { | ||
concat += element; | ||
}); | ||
return concat; | ||
}; | ||
return ConcatCommandHandler; | ||
}()); | ||
var StubCommandHandler = /** @class */ (function () { | ||
function StubCommandHandler() { | ||
} | ||
StubCommandHandler.prototype.execute = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
} | ||
class StubCommandHandler { | ||
execute(...args) { | ||
return undefined; | ||
}; | ||
return StubCommandHandler; | ||
}()); | ||
var NeverActiveStubCommandHandler = /** @class */ (function (_super) { | ||
__extends(NeverActiveStubCommandHandler, _super); | ||
function NeverActiveStubCommandHandler() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
NeverActiveStubCommandHandler.prototype.isEnabled = function () { | ||
} | ||
class NeverActiveStubCommandHandler extends StubCommandHandler { | ||
isEnabled() { | ||
return false; | ||
}; | ||
return NeverActiveStubCommandHandler; | ||
}(StubCommandHandler)); | ||
} | ||
} | ||
//# sourceMappingURL=command.spec.js.map |
{ | ||
"name": "@gedit/command", | ||
"version": "0.1.67", | ||
"version": "0.1.68", | ||
"license": "MIT", | ||
@@ -12,3 +12,3 @@ "main": "lib/common/index", | ||
"dependencies": { | ||
"@gedit/utils": "^0.1.69" | ||
"@gedit/utils": "^0.1.70" | ||
}, | ||
@@ -25,3 +25,3 @@ "scripts": { | ||
}, | ||
"gitHead": "1bb1a2c9de07b987e6e1c15053e7278766cbe23c" | ||
"gitHead": "8bdf36b3a3898da871ec15be92760865fc6a182a" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
73173
-18.47%1377
-17.35%Updated