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

Add missing editor/folder specific title vars #1093

Merged
merged 5 commits into from
Dec 27, 2019
Merged
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
111 changes: 67 additions & 44 deletions src/Store/TitleStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,81 @@ module Core = Oni_Core;
module Model = Oni_Model;

module Actions = Model.Actions;
module Option = Core.Utility.Option;
module Path = Core.Utility.Path;

let getDirectory = (fp: string): option(string) => {
let dirs =
Filename.dirname(fp) |> String.split_on_char(Filename.dir_sep.[0]);

List.length(dirs) - 1 |> List.nth_opt(dirs);
};
let withTag = (tag: string, value: option(string)) =>
Option.map(v => (tag, v), value);

let getTemplateVariables: Model.State.t => Core.StringMap.t(string) =
state => {
let initialValues = [("appName", "Onivim 2")];

let initialValues =
switch (Model.Selectors.getActiveBuffer(state)) {
| None => initialValues
| Some(buf) =>
let fp = Core.Buffer.getFilePath(buf);
let ret =
switch (fp) {
| None => initialValues
| Some(fp) =>
let activeEditorShort = Filename.basename(fp);
let parentDir = getDirectory(fp);

let initialValues = [
("activeEditorShort", activeEditorShort),
("activeEditorLong", fp),
...initialValues,
];

switch (parentDir) {
| None => initialValues
| Some(dir) => [("activeFolderShort", dir), ...initialValues]
};
};
switch (Core.Buffer.isModified(buf)) {
| false => ret
| true => [("dirty", "*"), ...ret]
};
};
let buffer = Model.Selectors.getActiveBuffer(state);
let filePath = Option.bind(Core.Buffer.getFilePath, buffer);

let appName = Option.some("Onivim 2") |> withTag("appName");

let dirty =
Option.map(Core.Buffer.isModified, buffer)
|> (
Copy link
Member

Choose a reason for hiding this comment

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

I like the way you refactored out the withTag method and pipe into fun here - nice! 👍

fun
| Some(true) => Some("*")
| _ => None
)
|> withTag("dirty");

let initialValues =
let (rootName, rootPath) =
switch (state.workspace) {
| None => initialValues
| Some(workspace) => [
("rootName", workspace.rootName),
("rootPath", workspace.workingDirectory),
...initialValues,
]
| Some({rootName, workingDirectory}) => (
Some(rootName) |> withTag("rootName"),
Some(workingDirectory) |> withTag("rootPath"),
)
| None => (None, None)
};

initialValues |> List.to_seq |> Core.StringMap.of_seq;
let activeEditorShort =
Option.map(Filename.basename, filePath) |> withTag("activeEditorShort");
let activeEditorMedium =
filePath
|> Option.bind(fp =>
switch (rootPath) {
| Some((_, base)) => Some(Path.toRelative(~base, fp))
| _ => None
}
)
|> withTag("activeEditorMedium");
let activeEditorLong = filePath |> withTag("activeEditorLong");

let activeFolderShort =
Option.(filePath |> map(Filename.dirname) |> map(Filename.basename))
|> withTag("activeFolderShort");
let activeFolderMedium =
filePath
|> Option.map(Filename.dirname)
|> Option.bind(fp =>
switch (rootPath) {
| Some((_, base)) => Some(Path.toRelative(~base, fp))
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the CI was failing because it expected ~base to be a named parameter. I made that quick change - 🤞 it passes now.

| _ => None
}
)
|> withTag("activeFolderMedium");
let activeFolderLong =
filePath |> Option.map(Filename.dirname) |> withTag("activeFolderLong");

[
appName,
dirty,
activeEditorShort,
activeEditorMedium,
activeEditorLong,
activeFolderShort,
activeFolderMedium,
activeFolderLong,
rootName,
rootPath,
]
|> Option.values
|> List.to_seq
|> Core.StringMap.of_seq;
};

module Effects = {
Expand Down