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

Refactor: Bring over additional types from Onivim 2 #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 3 additions & 6 deletions src/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ let start =
Log.info("Ready");

incr(lastRequestId);
let requestId = lastRequestId^;
send(Outgoing.Initialize({requestId, initData}));
send(Outgoing.Initialize({requestId: lastRequestId^, initData}));
handler(Ready) |> ignore;

| Incoming.Initialized =>
Expand All @@ -46,10 +45,9 @@ let start =
"ExtHostConfiguration" |> Handlers.stringToId |> Option.get;

incr(lastRequestId);
let requestId = lastRequestId^;
send(
Outgoing.RequestJSONArgs({
requestId,
requestId: lastRequestId^,
rpcId,
method: "$initializeConfiguration",
args:
Expand All @@ -62,11 +60,10 @@ let start =
handler(Initialized) |> ignore;

incr(lastRequestId);
let requestId = lastRequestId^;
let rpcId = "ExtHostWorkspace" |> Handlers.stringToId |> Option.get;
send(
Outgoing.RequestJSONArgs({
requestId,
requestId: lastRequestId^,
rpcId,
method: "$initializeWorkspace",
args: `List([]),
Expand Down
24 changes: 24 additions & 0 deletions src/Core/KeyedStringMap.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Key: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, and the interface, seems to be duplicated in Types.

type t =
pri {
hash: int,
name: string,
};
let compare: (t, t) => int;
let create: string => t;
} = {
type t = {
hash: int,
name: string,
};

let compare = (a, b) =>
a.hash == b.hash ? compare(a.name, b.name) : compare(a.hash, b.hash);

let create = name => {hash: Hashtbl.hash(name), name};
};

include Map.Make(Key);

let key = Key.create;
let keyName = (key: key) => key.name;
39 changes: 39 additions & 0 deletions src/Core/KeyedStringMap.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type key;
let key: string => key;
let keyName: key => string;

type t(+'a);

let empty: t('a);
let is_empty: t('a) => bool;

let mem: (key, t('a)) => bool;
let add: (key, 'a, t('a)) => t('a);
let update: (key, option('a) => option('a), t('a)) => t('a);
let remove: (key, t('a)) => t('a);
let merge:
((key, option('a), option('b)) => option('c), t('a), t('b)) => t('c);
let union: ((key, 'a, 'a) => option('a), t('a), t('a)) => t('a);

let compare: (('a, 'a) => int, t('a), t('a)) => int;
let equal: (('a, 'a) => bool, t('a), t('a)) => bool;

let iter: ((key, 'a) => unit, t('a)) => unit;
let fold: ((key, 'a, 'b) => 'b, t('a), 'b) => 'b;

let for_all: ((key, 'a) => bool, t('a)) => bool;
let exists: ((key, 'a) => bool, t('a)) => bool;

let filter: ((key, 'a) => bool, t('a)) => t('a);
let partition: ((key, 'a) => bool, t('a)) => (t('a), t('a));

let find_opt: (key, t('a)) => option('a);
let find_first_opt: (key => bool, t('a)) => option((key, 'a));
let find_last_opt: (key => bool, t('a)) => option((key, 'a));

let map: ('a => 'b, t('a)) => t('b);
let mapi: ((key, 'a) => 'b, t('a)) => t('b);

let to_seq: t('a) => Seq.t((key, 'a));
let add_seq: (Seq.t((key, 'a)), t('a)) => t('a);
let of_seq: Seq.t((key, 'a)) => t('a);
6 changes: 6 additions & 0 deletions src/Core/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(library
(name Core)
(public_name vscode-exthost.core)
(library_flags (-linkall))
(libraries luv yojson decoders-yojson)
(preprocess (pps ppx_deriving.show ppx_deriving_yojson)))
Comment on lines +1 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should have been removed entirely?

62 changes: 55 additions & 7 deletions src/Extension/Contributions.re
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* Contributions.re
* ExtensionContributions.re
*
* Types for VSCode Extension contribution points
*/

open Types;
open Rench;

Expand All @@ -13,6 +12,7 @@ module Command = {
command: string,
title: LocalizedToken.t,
category: option(string),
condition: WhenExpr.t,
};

let decode =
Expand All @@ -22,6 +22,12 @@ module Command = {
command: field.required("command", string),
title: field.required("title", LocalizedToken.decode),
category: field.optional("category", string),
condition:
field.withDefault(
"when",
WhenExpr.Value(True),
string |> map(WhenExpr.parse),
),
}
)
);
Expand All @@ -36,6 +42,39 @@ module Command = {
);
};

module Menu = {
[@deriving show]
type t = Menu.Schema.definition

and item = Menu.Schema.item;

module Decode = {
open Json.Decode;

let whenExpression = string |> map(WhenExpr.parse);

let item =
obj(({field, _}) =>
Menu.Schema.{
command: field.required("command", string),
alt: field.optional("alt", string),
group: field.optional("group", string),
index: None,
isVisibleWhen:
field.withDefault(
"when",
WhenExpr.Value(True),
string |> map(WhenExpr.parse),
),
}
);

let menus =
key_value_pairs(list(item))
|> map(List.map(((id, items)) => Menu.Schema.{id, items}));
};
};

module Configuration = {
[@deriving show]
type t = list(property)
Expand Down Expand Up @@ -70,6 +109,11 @@ module Configuration = {
};

let decode = Decode.configuration;

let toSettings = config =>
config
|> List.map(({name, default}) => (name, default))
|> Config.Settings.fromList;
};

module Language = {
Expand Down Expand Up @@ -205,48 +249,52 @@ module IconTheme = {

[@deriving show]
type t = {
configuration: Configuration.t,
commands: list(Command.t),
menus: list(Menu.t),
languages: list(Language.t),
grammars: list(Grammar.t),
themes: list(Theme.t),
iconThemes: list(IconTheme.t),
configuration: Configuration.t,
};

let default = {
configuration: [],
commands: [],
menus: [],
languages: [],
grammars: [],
themes: [],
iconThemes: [],
configuration: [],
};

let decode =
Json.Decode.(
obj(({field, _}) =>
{
configuration:
field.withDefault("configuration", [], Configuration.decode),
commands: field.withDefault("commands", [], list(Command.decode)),
menus: field.withDefault("menus", [], Menu.Decode.menus),
languages: field.withDefault("languages", [], list(Language.decode)),
grammars: field.withDefault("grammars", [], list(Grammar.decode)),
themes: field.withDefault("themes", [], list(Theme.decode)),
iconThemes:
field.withDefault("iconThemes", [], list(IconTheme.decode)),
configuration:
field.withDefault("configuration", [], Configuration.decode),
}
)
);

let encode = data =>
Json.Encode.(
obj([
("configuration", null),
("commands", data.commands |> list(Command.encode)),
("menus", null),
("languages", data.languages |> list(Language.encode)),
("grammars", data.grammars |> list(Grammar.encode)),
("themes", data.themes |> list(Theme.encode)),
("iconThemes", data.iconThemes |> list(IconTheme.encode)),
("configuration", null),
])
);

Expand Down
12 changes: 11 additions & 1 deletion src/Extension/Extension.rei
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Contributions: {
command: string,
title: LocalizedToken.t,
category: option(string),
condition: WhenExpr.t,
};
};

Expand All @@ -61,6 +62,8 @@ module Contributions: {
};
};

module Menu: {type t = Types.Menu.Schema.definition;};

module Grammar: {
[@deriving show]
type t = {
Expand Down Expand Up @@ -91,13 +94,16 @@ module Contributions: {

[@deriving show]
type t = {
configuration: Configuration.t,
commands: list(Command.t),
menus: list(Menu.t),
languages: list(Language.t),
grammars: list(Grammar.t),
themes: list(Theme.t),
iconThemes: list(IconTheme.t),
configuration: Configuration.t,
};

let encode: Types.Json.encoder(t);
};

module Manifest: {
Expand Down Expand Up @@ -128,6 +134,10 @@ module Manifest: {
| Workspace;

let decode: Types.Json.decoder(t);

module Encode: {let kind: Types.Json.encoder(kind);};

let getDisplayName: t => string;
};

module Scanner: {
Expand Down
1 change: 0 additions & 1 deletion src/Extension/Manifest.re
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ let getDisplayName = (manifest: t) => {

let remapPaths = (rootPath: string, manifest: t) => {
...manifest,
//main: Option.map(Path.join(rootPath), manifest.main),
icon: Option.map(Path.join(rootPath), manifest.icon),
contributes: Contributions.remapPaths(rootPath, manifest.contributes),
};
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name Extension)
(public_name vscode-exthost.extension)
(libraries Rench luv re timber yojson decoders-yojson vscode-exthost.types)
(libraries Rench luv re timber yojson decoders-yojson vscode-exthost.whenexpr vscode-exthost.types)
(preprocess (pps ppx_deriving.show ppx_deriving_yojson)))
2 changes: 2 additions & 0 deletions src/Exthost.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module NamedPipe = NamedPipe;

module Msg = Msg;

module Extension = Extension;
module Protocol = Protocol;
module Transport = Transport;
module Utility = Utility;
module WhenExpr = WhenExpr;
2 changes: 2 additions & 0 deletions src/Exthost.rei
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ module Request: {
};
};

module Extension = Extension;
module Protocol = Protocol;
module Transport = Transport;
module Utility = Utility;
module WhenExpr = WhenExpr;
59 changes: 59 additions & 0 deletions src/Types/Command.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module Log = (val Timber.Log.withNamespace("Oni2.Core.Command"));

[@deriving show]
type t('msg) = {
id: string,
title: option(string),
category: option(string),
icon: option([@opaque] IconTheme.IconDefinition.t),
isEnabledWhen: WhenExpr.t,
msg: [ | `Arg0('msg) | `Arg1(Json.t => 'msg)],
};

let map = (f, command) => {
...command,
msg:
switch (command.msg) {
| `Arg0(msg) => `Arg0(f(msg))
| `Arg1(msgf) => `Arg1(arg => f(msgf(arg)))
},
};

module Lookup = {
type command('msg) = t('msg);
type t('msg) = KeyedStringMap.t(command('msg));

let fromList = commands =>
commands
|> List.to_seq
|> Seq.map(command => (KeyedStringMap.key(command.id), command))
|> KeyedStringMap.of_seq;

let get = (key, lookup) =>
KeyedStringMap.find_opt(KeyedStringMap.key(key), lookup);

let add = (key, command, lookup) =>
KeyedStringMap.add(KeyedStringMap.key(key), command, lookup);

let union = (xs, ys) =>
KeyedStringMap.union(
(key, _x, y) => {
Log.warnf(m =>
m("Encountered duplicate command: %s", KeyedStringMap.keyName(key))
);
Some(y);
},
xs,
ys,
);

let unionMany = lookups =>
List.fold_left(union, KeyedStringMap.empty, lookups);

let map = (f, lookup) => KeyedStringMap.map(map(f), lookup);

let toList = lookup =>
KeyedStringMap.to_seq(lookup)
|> Seq.map(((_key, definition)) => definition)
|> List.of_seq;
};
Loading