Skip to content

Commit

Permalink
Feature/configuration from json (onivim#88)
Browse files Browse the repository at this point in the history
Read the configuration out of a file using `yojson` initial work for having a user editable configuration file
  • Loading branch information
akinsho authored Mar 1, 2019
1 parent 27d2af3 commit 61fc721
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 31 deletions.
5 changes: 5 additions & 0 deletions assets/configuration/configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"editor.lineNumbers": ["relative"],
"editor.minimap.enabled": true,
"editor.tablineMode": ["tabs"]
}
12 changes: 8 additions & 4 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ case "${machine}" in
NODE_PATH="$(pwd)/vendor/node-v10.15.1/linux-x64/node"
TEXTMATE_SERVICE_PATH="$(pwd)/src/textmate_service/lib/index.js"
EXTENSIONS_PATH="$(pwd)/extensions"
NEOVIM_PATH="$(pwd)/vendor/neovim-0.3.3/nvim-linux64/bin/nvim";;
NEOVIM_PATH="$(pwd)/vendor/neovim-0.3.3/nvim-linux64/bin/nvim"
CONFIGURATION_PATH="$(pwd)/assets/configuration/configuration.json";;
Mac)
NODE_PATH="$(pwd)/vendor/node-v10.15.1/osx/node"
TEXTMATE_SERVICE_PATH="$(pwd)/src/textmate_service/lib/index.js"
EXTENSIONS_PATH="$(pwd)/extensions"
NEOVIM_PATH="$(pwd)/vendor/neovim-0.3.3/nvim-osx64/bin/nvim";;
NEOVIM_PATH="$(pwd)/vendor/neovim-0.3.3/nvim-osx64/bin/nvim"
CONFIGURATION_PATH="$(pwd)/assets/configuration/configuration.json";;
*)
NEOVIM_PATH="$(pwd)/vendor/neovim-0.3.3/nvim-win64/bin/nvim.exe"
NEOVIM_PATH="$(cygpath -m "$NEOVIM_PATH")"
Expand All @@ -50,10 +52,12 @@ case "${machine}" in
TEXTMATE_SERVICE_PATH="$(pwd)/src/textmate_service/lib/index.js"
TEXTMATE_SERVICE_PATH="$(cygpath -m "$TEXTMATE_SERVICE_PATH")"
NODE_PATH="$(pwd)/vendor/node-v10.15.1/win-x64/node.exe"
NODE_PATH="$(cygpath -m "$NODE_PATH")";;
NODE_PATH="$(cygpath -m "$NODE_PATH")"
CONFIGURATION_PATH="$(pwd)/assets/configuration/configuration.json"
CONFIGURATION_PATH="$(cygpath -m "$CONFIGURATION_PATH")";;
esac

oni_bin_path="{neovim:\"$NEOVIM_PATH\",node:\"$NODE_PATH\",textmateService:\"$TEXTMATE_SERVICE_PATH\",bundledExtensions:\"$EXTENSIONS_PATH\"}"
oni_bin_path="{neovim:\"$NEOVIM_PATH\",node:\"$NODE_PATH\",configuration:\"$CONFIGURATION_PATH\",textmateService:\"$TEXTMATE_SERVICE_PATH\",bundledExtensions:\"$EXTENSIONS_PATH\"}"

# create the current bin path as this might not exist yet
if [ ! -d "$config_path" ]; then
Expand Down
39 changes: 29 additions & 10 deletions src/editor/Core/Configuration.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@
* Configuration settings for the editor
*/

type tablineMode =
| Buffers
| Tabs
| Hybrid;
[@deriving (show({with_path: false}), yojson)]
type editorTablineMode =
| [@name "buffers"] Buffers
| [@name "tabs"] Tabs
| [@name "hybrid"] Hybrid;

[@deriving (show({with_path: false}), yojson({strict: false}))]
type t = {
[@key "editor.lineNumbers"]
editorLineNumbers: LineNumber.setting,
[@key "editor.minimap.enabled"]
editorMinimapEnabled: bool,
tablineMode,
[@key "editor.tablineMode"]
editorTablineMode,
};

let create: unit => t =
() => {
editorLineNumbers: On,
editorMinimapEnabled: true,
tablineMode: Buffers,
let default = {
editorMinimapEnabled: true,
editorTablineMode: Buffers,
editorLineNumbers: Relative,
};

let ofFile = filePath => Yojson.Safe.from_file(filePath) |> of_yojson;

let getConfigPath = () => {
let {configPath, _}: Setup.t = Setup.init();
configPath;
};

let create = (~configPath=getConfigPath(), ()) =>
switch (ofFile(configPath)) {
| Ok(config) => config
| Error(loc) =>
print_endline("Error Loc: " ++ loc);
default;
};
10 changes: 5 additions & 5 deletions src/editor/Core/LineNumber.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* Settings and utilities for managing Linumbers
*/

[@deriving (yojson, show)]
type setting =
| On
| Off
| Relative;
| [@name "on"] On
| [@name "off"] Off
| [@name "relative"] Relative;

/*
* Given a number of lines, gives the number of digits
Expand All @@ -30,7 +31,7 @@ let getLineNumberPixelWidth = (~lines: int, ~fontPixelWidth: int, ()) => {
};

let getLineNumber =
(~bufferLine: int, ~cursorLine: int, ~setting: setting, ()) => {
(~bufferLine: int, ~cursorLine: int, ~setting: setting, ()) =>
switch (setting) {
| Relative =>
if (bufferLine === cursorLine) {
Expand All @@ -40,4 +41,3 @@ let getLineNumber =
}
| _ => bufferLine
};
};
1 change: 1 addition & 0 deletions src/editor/Core/Oni_Core.re
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ module Tokenizer = Tokenizer;
module Types = Types;
module Utility = Utility;
module Wildmenu = Wildmenu;
module Configuration = Configuration;
4 changes: 2 additions & 2 deletions src/editor/Core/Reducer.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let truncateFilepath = path =>
};

let showTablineTabs = (state: State.t, tabs) =>
switch (state.configuration.tablineMode) {
switch (state.configuration.editorTablineMode) {
| Tabs =>
List.map(
({tab, name}: Tabline.t) =>
Expand All @@ -29,7 +29,7 @@ let showTablineTabs = (state: State.t, tabs) =>
};

let showTablineBuffers = (state: State.t, buffers, activeBufferId) =>
switch (state.configuration.tablineMode) {
switch (state.configuration.editorTablineMode) {
| Buffers =>
List.map(
({id, filePath, modified, _}: BufferMetadata.t) =>
Expand Down
15 changes: 6 additions & 9 deletions src/editor/Core/Setup.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Runtime configuration of dependencies
*/

[@deriving yojson({strict: false, exn: true})]
[@deriving (show, yojson({strict: false, exn: true}))]
type t = {
[@key "neovim"]
neovimPath: string,
Expand All @@ -14,16 +14,13 @@ type t = {
textmateServicePath: string,
[@key "bundledExtensions"]
bundledExtensionsPath: string,
[@key "configuration"]
configPath: string,
};

let ofString = s => {
Yojson.Safe.from_string(s) |> of_yojson_exn;
};
let ofString = str => Yojson.Safe.from_string(str) |> of_yojson_exn;

let ofFile = filePath => {
Pervasives.open_in(filePath) |> Pervasives.input_line |> ofString;
};
let ofFile = filePath => Yojson.Safe.from_file(filePath) |> of_yojson_exn;

let init = () => {
let init = () =>
Revery.Environment.getExecutingDirectory() ++ "setup.json" |> ofFile;
};
1 change: 1 addition & 0 deletions src/editor/Core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(name Oni_Core)
(public_name Oni_Core)
(libraries
str
bigarray
zed
reglfw
Expand Down
3 changes: 2 additions & 1 deletion test/editor/Core/SetupTests.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ open TestFramework;

describe("Setup", ({test, _}) =>
test("ofString", ({expect}) => {
let setupInfo = "{neovim:\"/path/to/neovim\",node:\"/path/to/node\",textmateService:\"/path/to/textmate\",bundledExtensions:\"/path/to/extensions\"}";
let setupInfo = "{neovim:\"/path/to/neovim\",node:\"/path/to/node\",textmateService:\"/path/to/textmate\",bundledExtensions:\"/path/to/extensions\",configuration:\"/path/to/config\"}";
let setup = Setup.ofString(setupInfo);
expect.string(setup.neovimPath).toEqual("/path/to/neovim");
expect.string(setup.nodePath).toEqual("/path/to/node");
expect.string(setup.textmateServicePath).toEqual("/path/to/textmate");
expect.string(setup.bundledExtensionsPath).toEqual(
"/path/to/extensions",
);
expect.string(setup.configPath).toEqual("/path/to/config");
})
);

0 comments on commit 61fc721

Please sign in to comment.