forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/command palette (onivim#138)
* Add initial infrastructure for Command Palette * Fix palette input sizing, fix open in command-palette module * Add input control mode to state add commands module and ability to close menu * Allow for opening and closing palette Fix multi input bug, make palette actions more versatile * Split out palette reducer, move control mode responsibility to the commands * update todos * Create Keybindings module * Derive keybindings from json file * Pass effects from NeovimApi to the command palette * Add working handlers for command palette select * Rename condition to when to match vscode syntax * Change input control naming to match vscode * run esy format * Fix setup tests
- Loading branch information
Showing
17 changed files
with
321 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"bindings": [ | ||
{ "key": "<C-P>", "command": "commandPalette.open", "when": ["editorTextFocus"] }, | ||
{ "key": "<ESC>", "command": "commandPalette.close", "when": ["commandPaletteFocus"] }, | ||
{ "key": "<C-N>", "command": "commandPalette.next", "when": ["commandPaletteFocus"] }, | ||
{ "key": "<C-P>", "command": "commandPalette.previous", "when": ["commandPaletteFocus"] }, | ||
{ "key": "<CR>", "command": "commandPalette.select", "when": ["commandPaletteFocus"] } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
open Types; | ||
|
||
type t = Palette.t; | ||
open Palette; | ||
|
||
let join = paths => { | ||
let sep = Filename.dir_sep; | ||
List.fold_left((accum, p) => accum ++ sep ++ p, "", paths); | ||
}; | ||
|
||
let openConfigurationFile = (effects: Effects.t) => { | ||
let path = | ||
join([ | ||
Revery.Environment.getWorkingDirectory(), | ||
"assets", | ||
"configuration", | ||
"configuration.json", | ||
]); | ||
effects.openFile(~path, ()); | ||
}; | ||
|
||
let openKeybindingsFile = (effects: Effects.t) => { | ||
let path = | ||
join([ | ||
Revery.Environment.getWorkingDirectory(), | ||
"assets", | ||
"configuration", | ||
"keybindings.json", | ||
]); | ||
effects.openFile(~path, ()); | ||
}; | ||
|
||
let commandPaletteCommands = (effects: Effects.t) => [ | ||
{ | ||
name: "Open configuration file", | ||
command: () => openConfigurationFile(effects), | ||
}, | ||
{ | ||
name: "Open keybindings file", | ||
command: () => openKeybindingsFile(effects), | ||
}, | ||
]; | ||
|
||
let create = (~effects: option(Effects.t)=?, ()) => | ||
switch (effects) { | ||
| Some(e) => { | ||
isOpen: false, | ||
commands: commandPaletteCommands(e), | ||
selectedItem: 0, | ||
} | ||
| None => {isOpen: false, commands: [], selectedItem: 0} | ||
}; | ||
|
||
let make = (~effects: Effects.t) => | ||
create(~effects, ()) |> (c => Actions.CommandPaletteStart(c.commands)); | ||
|
||
let position = (selectedItem, change, commands: list(command)) => | ||
selectedItem + change >= List.length(commands) ? 0 : selectedItem + change; | ||
|
||
let reduce = (state: t, action: Actions.t) => | ||
switch (action) { | ||
| CommandPaletteStart(commands) => {...state, commands} | ||
| CommandPalettePosition(pos) => { | ||
...state, | ||
selectedItem: position(state.selectedItem, pos, state.commands), | ||
} | ||
| CommandPaletteOpen => {...state, isOpen: true} | ||
| CommandPaletteClose => {...state, isOpen: false} | ||
| CommandPaletteSelect => | ||
/** | ||
TODO: Refactor this to middleware so this action is handled like a redux side-effect | ||
as this makes the reducer impure | ||
*/ | ||
let selected = List.nth(state.commands, state.selectedItem); | ||
selected.command(); | ||
{...state, isOpen: false}; | ||
| _ => state | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
type oniCommand = { | ||
name: string, | ||
command: unit => list(Actions.t), | ||
}; | ||
|
||
type t = list(oniCommand); | ||
|
||
let oniCommands = [ | ||
{ | ||
name: "commandPalette.open", | ||
command: _ => [ | ||
CommandPaletteOpen, | ||
SetInputControlMode(CommandPaletteFocus), | ||
], | ||
}, | ||
{ | ||
name: "commandPalette.close", | ||
command: _ => [ | ||
CommandPaletteClose, | ||
SetInputControlMode(EditorTextFocus), | ||
], | ||
}, | ||
{name: "commandPalette.next", command: _ => [CommandPalettePosition(1)]}, | ||
{ | ||
name: "commandPalette.previous", | ||
command: _ => [CommandPalettePosition(-1)], | ||
}, | ||
{ | ||
name: "commandPalette.select", | ||
command: _ => [ | ||
CommandPaletteSelect, | ||
SetInputControlMode(EditorTextFocus), | ||
], | ||
}, | ||
]; | ||
|
||
let handleCommand = (~commands=oniCommands, name) => { | ||
let matchingCmd = List.find_opt(cmd => name == cmd.name, commands); | ||
switch (matchingCmd) { | ||
| Some(c) => c.command() | ||
| None => [Noop] | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
open Actions; | ||
open Types; | ||
|
||
[@deriving show] | ||
type t = { | ||
id: int, | ||
scrollX: int, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
open Types.Input; | ||
|
||
[@deriving (show, yojson({strict: false, exn: false}))] | ||
type keyBindings = { | ||
key: string, | ||
command: string, | ||
[@key "when"] | ||
condition: controlMode, | ||
}; | ||
|
||
[@deriving (show, yojson({strict: false, exn: false}))] | ||
type t = list(keyBindings); | ||
|
||
[@deriving (show, yojson({strict: false, exn: false}))] | ||
type json_keybindings = {bindings: t}; | ||
|
||
let ofFile = filePath => | ||
Yojson.Safe.from_file(filePath) |> json_keybindings_of_yojson; | ||
|
||
let get = () => { | ||
let {keybindingsPath, _}: Setup.t = Setup.init(); | ||
switch (ofFile(keybindingsPath)) { | ||
| Ok(b) => b.bindings | ||
| Error(e) => | ||
print_endline("Error parsing keybindings file ------- " ++ e); | ||
[]; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
open Revery; | ||
open Oni_Core; | ||
open Revery.UI; | ||
open Revery.UI.Components; | ||
|
||
let component = React.component("commandPalette"); | ||
|
||
let paletteWidth = 400; | ||
|
||
let containerStyles = (theme: Theme.t) => | ||
Style.[ | ||
backgroundColor(theme.colors.editorMenuBackground), | ||
color(theme.colors.editorMenuForeground), | ||
width(paletteWidth), | ||
height(300), | ||
boxShadow( | ||
~xOffset=-15., | ||
~yOffset=5., | ||
~blurRadius=30., | ||
~spreadRadius=5., | ||
~color=Color.rgba(0., 0., 0., 0.2), | ||
), | ||
]; | ||
|
||
let paletteItemStyle = Style.[fontSize(14)]; | ||
|
||
let createElement = | ||
(~children as _, ~commandPalette: CommandPalette.t, ~theme: Theme.t, ()) => | ||
component(hooks => | ||
( | ||
hooks, | ||
commandPalette.isOpen | ||
? <View style={containerStyles(theme)}> | ||
/* <Input style=Style.[width(paletteWidth)] /> */ | ||
|
||
<ScrollView style=Style.[height(350)]> | ||
...{List.mapi( | ||
(index, cmd: Types.Palette.command) => | ||
<MenuItem | ||
icon="" | ||
style=paletteItemStyle | ||
label={cmd.name} | ||
selected={index == commandPalette.selectedItem} | ||
theme | ||
/>, | ||
commandPalette.commands, | ||
)} | ||
</ScrollView> | ||
</View> | ||
: React.listToElement([]), | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.