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

Change the shortcut to be same as Sublime/Atom/etc. #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Brackets Commands Quick Search
==============================
Use the keyboard to quickly search for commands and run them:

1. Press Ctrl+Alt+? (⌃⌘? on Mac)
1. Press Ctrl+Shift+P (⇧⌘P on Mac)
<br>(or invoke regular Quick Open and type a "?")
2. Start typing (part of) the name of a command
3. Use arrow keys to select the one you want (or skip this to use the topmost result)
Expand All @@ -28,4 +28,4 @@ To install extensions:
MIT-licensed -- see `main.js` for details.

### Compatibility
Brackets Sprint 16 or newer (or Adobe Edge Code Preview 2 or newer).
Brackets Sprint 16 or newer (or Adobe Edge Code Preview 2 or newer).
69 changes: 33 additions & 36 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* Copyright (c) 2012 Peter Flynn.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -26,7 +26,7 @@

define(function (require, exports, module) {
"use strict";

// Brackets modules
var CommandManager = brackets.getModule("command/CommandManager"),
Commands = brackets.getModule("command/Commands"),
Expand All @@ -35,34 +35,34 @@ define(function (require, exports, module) {
EditorManager = brackets.getModule("editor/EditorManager"),
QuickOpen = brackets.getModule("search/QuickOpen"),
StringUtils = brackets.getModule("utils/StringUtils");


/** @type {Array.<{ id:string, name:string }>} */
var _commandList;

/**
* Editor that should be focused when executing the command (that had focus before opening search bar)
* @type {Editor}
*/
var whichEditor;


function ensureCommandList() {
if (_commandList) {
return;
}
_commandList = [];

// We don't know which of these commands have no required arguments. But we can safely
// assume that any commands attached to menus or keyboard shortcuts fit the bill.
var ids = CommandManager.getAll();

// Get list of all top-level menu bar menus
// Ignore context menus since it seems less safe to assume those commands can be run in isolation
var menuIds = $.map(Menus.AppMenuBar, function (menuConstVal, menuConstName) {
return menuConstVal;
});

// Filter command list accordingly
ids.forEach(function (id) {
var noArgsOk = false;
Expand All @@ -88,34 +88,34 @@ define(function (require, exports, module) {
}
});
}

function done() {
// No cleanup - keep cached list of commands for next invocation
}

/**
* @param {string} query User query/filter string
* @return {Array.<string>} Sorted and filtered results that match the query
*/
function search(query, matcher) {
ensureCommandList();

query = query.substr(1); // lose the "?" prefix

var stringMatch = (matcher && matcher.match) ? matcher.match.bind(matcher) : QuickOpen.stringMatch;

// Filter and rank how good each match is
var filteredList = $.map(_commandList, function (commandInfo) {

// TODO: filter out disabled commands?

var searchResult = stringMatch(commandInfo.name, query);
if (searchResult) {
searchResult.id = commandInfo.id;
}
return searchResult;
});

// Sort based on ranking & basic alphabetical order
QuickOpen.basicMatchSort(filteredList);

Expand All @@ -138,33 +138,33 @@ define(function (require, exports, module) {
function itemSelect(selectedItem) {
// Many commands are focus-sensitive, so we have to carefully make sure that focus is restored to
// the (correct) editor before running the command

// First wait for Quick Open to restore focus to the master editor
setTimeout(function () {
// Now set focus on the correct editor (which might be an inline editor)
if (whichEditor) {
whichEditor.focus();
whichEditor = null;
}

// One more timeout to wait for focus to move to that editor
setTimeout(function () {
CommandManager.execute(selectedItem.id);
}, 0);
}, 0);
}


/**
* Similar to default formatting, but with added text showing keybinding
*
*
* @param {SearchResult} fileEntry
* @param {string} query
* @return {string}
*/
function resultFormatter(item, query) {
var displayName = QuickOpen.highlightMatch(item);

// Show shortcut on right of item
// TODO: display multiple shortcuts
// TODO: display which menu it's in also
Expand All @@ -174,8 +174,8 @@ define(function (require, exports, module) {

return "<li>" + displayName + "<span style='float:right'>" + shortcut + "</span></li>";
}


// Register as a new Quick Open mode
QuickOpen.addQuickOpenPlugin(
{
Expand All @@ -191,23 +191,20 @@ define(function (require, exports, module) {
resultsFormatter: resultFormatter
}
);

function beginSearchForCommands() {
whichEditor = EditorManager.getFocusedEditor();

// Begin Quick Open in our search mode
QuickOpen.beginSearch("?");
}


// Register command as shortcut to launch this Quick Open mode
var SEARCH_COMMAND_ID = "pflynn.searchCommands";
CommandManager.register("Search Commands", SEARCH_COMMAND_ID, beginSearchForCommands);

var menu = Menus.getMenu(Menus.AppMenuBar.HELP_MENU);
menu.addMenuDivider(Menus.FIRST);
menu.addMenuItem(SEARCH_COMMAND_ID, [
{key: "Ctrl-Alt-/", displayKey: "Ctrl-Alt-?", platform: "win"},
{key: "Ctrl-Cmd-/", displayKey: "Ctrl-Cmd-?", platform: "mac"}
], Menus.FIRST);
menu.addMenuItem(SEARCH_COMMAND_ID, {key: "Ctrl-Shift-P", displayKey: "Ctrl-Shift-P"}, Menus.FIRST);
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"author": "Peter Flynn",
"version": "1.0.5",
"engines": { "brackets": ">=0.16" },
"description": "Search and execute commands by typing part of their name, similar to Quicksilver (or Sublime's Ctrl+Shift+P 'Command Palette' or Eclipse's Ctrl+3). Press Ctrl+Alt+? (\u2303\u2318? on Mac) to launch."
}
"description": "Search and execute commands by typing part of their name, similar to Quicksilver (or Sublime's Ctrl+Shift+P 'Command Palette' or Eclipse's Ctrl+3). Press Ctrl+Shift+P (\u21E7\u2318P on Mac) to launch."
}