Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Reduce dependencies #40

Merged
merged 4 commits into from
Dec 14, 2020
Merged
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: 0 additions & 4 deletions editor-extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
"command": "reason-language-server.restart",
"title": "Restart Reason Language Server"
},
{
"command": "reason-language-server.show_ppxed_source",
"title": "Reason: Show the fully ppxed source for this file."
},
{
"command": "reason-language-server.show_ast",
"title": "Reason: Show the abstract syntax tree (AST) for this file."
Expand Down
41 changes: 0 additions & 41 deletions editor-extensions/vscode/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,60 +221,19 @@ function activate(context) {
}
}

class PpxedSourceProvider {
constructor() {
this.privateOnDidChange = new vscode.EventEmitter()
this.onDidChange = this.privateOnDidChange.event;
}
provideTextDocumentContent(uri, token) {
if (!client) {
return Promise.reject("No language client running")
}

return client.sendRequest("custom:reasonLanguageServer/showPpxedSource", {
"textDocument": {
"uri": uri.with({scheme: 'file'}).toString(),
},
// unused currently
"position": {character: 0, line: 0},
})
}
}

const provider = new PpxedSourceProvider()
const astProvider = new AstSourceProvider()

context.subscriptions.push(
vscode.workspace.onDidSaveTextDocument((document) => {
const uri = document.uri;
provider.privateOnDidChange.fire(uri.with({scheme: 'ppxed-source'}))
astProvider.privateOnDidChange.fire(uri.with({scheme: 'ast-source'}))
}),
);

context.subscriptions.push(configureLanguage());

vscode.workspace.registerTextDocumentContentProvider("ppxed-source", provider);
vscode.workspace.registerTextDocumentContentProvider("ast-source", astProvider);

const showPpxedSource = () => {
if (!client) {
return vscode.window.showInformationMessage('Language server not running');
}
const editor = vscode.window.activeTextEditor;
if (!editor) {
return vscode.window.showInformationMessage('No active editor');
}
if (editor.document.languageId !== 'ocaml' && editor.document.languageId !== 'reason') {
return vscode.window.showInformationMessage('Not an OCaml or Reason file');
}

const document = TextDocument.create(editor.document.uri.with({scheme: 'ppxed-source'}), editor.document.languageId, 1, '');
vscode.window.showTextDocument(document);
};

vscode.commands.registerCommand('reason-language-server.show_ppxed_source', showPpxedSource);

vscode.commands.registerCommand('reason-language-server.dump_file_data', () => {
if (!client) {
return vscode.window.showInformationMessage('Language server not running');
Expand Down
23 changes: 0 additions & 23 deletions src/ppx/Ppx_Monads.re
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,6 @@ Alternatively, if you are just performing a side effect, and want
the result of the whole thing to be unit, use `let%consume`.
|};

let opt_wrap_explanation = {|
Optional declaration sugar:
```
let%opt_wrap name = value;
otherStuff
```
is transformed into
```
switch (value) {
| None => None
| Some(name) => Some({
otherStuff
})
}
```
The `wrap` suffix means that the `otherStuff` will be automatically
wrapped in a `Some`.

If you don't want this wrapping, then use `let%opt`.
Alternatively, if you are just performing a side effect, and want
the result of the whole thing to be unit, use `let%consume`.
|};

let opt_consume_explanation = {|
Optional declaration sugar:
Expand Down Expand Up @@ -143,7 +121,6 @@ let mapper =
) as txt, loc}, PStr([{pstr_desc: Pstr_eval({pexp_desc: Pexp_let(Nonrecursive, bindings, continuation)}, _attributes)}]))) => {
let (front, explanation) = switch (txt) {
| "opt" => ([%expr Monads.Option.bind], opt_explanation)
| "opt_wrap" => ([%expr Monads.Option.map], opt_wrap_explanation)
| "opt_consume" => ([%expr Monads.Option.consume], opt_consume_explanation)
| "try" => ([%expr Monads.Result.bind], "Sugar for the Result type")
| "try_wrap" => ([%expr Monads.Result.map], "Sugar for the Result type - auto-wraps in `Ok()`")
Expand Down
13 changes: 7 additions & 6 deletions src/rescript-editor-support/EditorSupportCommands.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open Infix;
module J = JsonShort;

let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
Expand Down Expand Up @@ -51,10 +50,12 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
~markdown=!state.settings.clientNeedsPlainText,
~showPath=state.settings.showModulePathOnHover,
loc,
)
|? "";
);
let hover =
hoverText == "" ? [] : [("hover", dedupHover(hoverText, i))];
switch (hoverText) {
| None => []
| Some(s) => [("hover", dedupHover(s, i))]
};

let uriLocOpt =
References.definitionForLoc(
Expand Down Expand Up @@ -134,7 +135,7 @@ let dump = files => {
files
|> List.iter(pathWithPos => {
let (filePath, selectPos) = pathWithPos |> splitLineChar;
let filePath = maybeConcat(Unix.getcwd(), filePath);
let filePath = Files.maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
let result =
switch (State.getFullFromCmt(uri, state)) {
Expand Down Expand Up @@ -174,7 +175,7 @@ let complete = (~pathWithPos, ~currentFile) => {
};
switch (pathWithPos |> splitLineChar) {
| (filePath, Some(pos)) =>
let filePath = maybeConcat(Unix.getcwd(), filePath);
let filePath = Files.maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
let result =
switch (State.getFullFromCmt(uri, state)) {
Expand Down
20 changes: 20 additions & 0 deletions src/rescript-editor-support/Files.re
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,23 @@ let rec collect = (~checkDir=_ => true, path, test) =>
}
| _ => test(path) ? [path] : []
};

let fileConcat = (a, b) =>
if (b != ""
&& b.[0] == '.'
&& String.length(b) >= 2
&& b.[1] == Filename.dir_sep.[0]) {
Filename.concat(a, String.sub(b, 2, String.length(b) - 2));
} else {
Filename.concat(a, b);
};

let isFullPath = b =>
b.[0] == '/' || Sys.win32 && String.length(b) > 1 && b.[1] == ':';

let maybeConcat = (a, b) =>
if (b != "" && isFullPath(b)) {
b;
} else {
fileConcat(a, b);
};
4 changes: 2 additions & 2 deletions src/rescript-editor-support/FindFiles.re
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ let findProjectFiles =
(~debug, namespace, root, sourceDirectories, compiledBase) => {
let files =
sourceDirectories
|> List.map(Infix.fileConcat(root))
|> List.map(Files.fileConcat(root))
|> ifDebug(debug, "Source directories", String.concat(" - "))
|> List.map(name => Files.collect(name, isSourceFile))
|> List.concat
Expand Down Expand Up @@ -308,7 +308,7 @@ let findDependencyFiles = (~debug, base, config) => {
Log.log("Compiled base: " ++ compiledBase);
};
let compiledDirectories =
directories |> List.map(Infix.fileConcat(compiledBase));
directories |> List.map(Files.fileConcat(compiledBase));
let compiledDirectories =
namespace == None
? compiledDirectories
Expand Down
22 changes: 1 addition & 21 deletions src/rescript-editor-support/Infix.re
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ let (|?<) = (o, fn) =>
| Some(v) => fn(v)
};

let fileConcat = (a, b) =>
if (b != ""
&& b.[0] == '.'
&& String.length(b) >= 2
&& b.[1] == Filename.dir_sep.[0]) {
Filename.concat(a, String.sub(b, 2, String.length(b) - 2));
} else {
Filename.concat(a, b);
};

let logIfAbsent = (message, x) =>
switch (x) {
| None =>
Expand All @@ -70,14 +60,4 @@ let logIfAbsent = (message, x) =>
| _ => x
};

let isFullPath = b =>
b.[0] == '/' || Sys.win32 && String.length(b) > 1 && b.[1] == ':';

let maybeConcat = (a, b) =>
if (b != "" && isFullPath(b)) {
b;
} else {
fileConcat(a, b);
};

let (/+) = fileConcat;
let (/+) = Files.fileConcat;
96 changes: 22 additions & 74 deletions src/rescript-editor-support/MessageHandlers.re
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,28 @@ let handlers:
let%try (uri, pos) = Protocol.rPositionParams(params);
let%try package = getPackage(uri, state);

let res =
{
let pos = Utils.cmtLocFromVscode(pos);
let%opt (file, extra) =
State.fileForUri(state, ~package, uri) |> toOptionAndLog;

let%opt_wrap refs = References.refsForPos(~file, ~extra, pos);
(
state,
J.l(
refs
|> List.map(loc =>
J.o([
("range", Protocol.rangeOfLoc(loc)),
("kind", J.i(2)),
])
),
),
);
}
|? (state, Json.Null);

Ok(res);
let pos = Utils.cmtLocFromVscode(pos);
let refs =
switch (State.fileForUri(state, ~package, uri) |> toOptionAndLog) {
| None => None
| Some((file, extra)) => References.refsForPos(~file, ~extra, pos)
};
Ok((
state,
switch (refs) {
| None => J.null
| Some(refs) =>
J.l(
refs
|> List.map(loc =>
J.o([
("range", Protocol.rangeOfLoc(loc)),
("kind", J.i(2)),
])
),
)
},
));
},
),
(
Expand Down Expand Up @@ -749,57 +748,6 @@ let handlers:
Ok((state, Json.Null));
},
),
(
"custom:reasonLanguageServer/showPpxedSource",
(state, params) => {
let%try (uri, _pos) = Protocol.rPositionParams(params);
let%try package = getPackage(uri, state);
let%try (file, _extra) = State.fileForUri(state, ~package, uri);
let%try parsetree =
AsYouType.getParsetree(
~uri,
~moduleName=file.moduleName,
~cacheLocation=package.tmpPath,
);
if (State.isMl(uri)) {
switch (parsetree) {
| `Implementation(str) =>
Pprintast.structure(Format.str_formatter, str)
| `Interface(int) => Pprintast.signature(Format.str_formatter, int)
};
} else {
module Convert =
Migrate_parsetree.Convert(
Migrate_parsetree.OCaml_406,
Migrate_parsetree.OCaml_408,
);
switch (parsetree) {
| `Implementation(str) =>
Reason_toolchain.RE.print_implementation_with_comments(
Format.str_formatter,
(Convert.copy_structure(str), []),
)
| `Interface(int) =>
Reason_toolchain.RE.print_interface_with_comments(
Format.str_formatter,
(Convert.copy_signature(int), []),
)
};
};
let source = Format.flush_str_formatter();

/* let source = State.isMl(uri) ? source : switch (package.refmtPath) {
| None => source
| Some(refmt) =>
let interface = Utils.endsWith(uri, "i");
switch (AsYouType.convertToRe(~formatWidth=None, ~interface, source, refmt)) {
| RResult.Error(_) => source
| Ok(s) => s
}
}; */
Ok((state, Json.String(source)));
},
),
(
"custom:reasonLanguageServer/showAst",
(state, params) => {
Expand Down
15 changes: 1 addition & 14 deletions src/rescript-editor-support/Monads.re
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@

module type MonadThing = {
type t('a);
let map: (t('a), ~f: 'a => 'b) => t('b);
let bind: (t('a), ~f: 'a => t('b)) => t('b);
let consume: (t('a), ~f: 'a => unit) => unit;
};

module Option = {
type t('a) = option('a);
let map = (value, ~f as use) =>
switch (value) {
| Some(x) => Some(use(x))
| None => None
};
let bind = (value, ~f as use) =>
switch (value) {
| Some(x) => use(x)
Expand All @@ -25,7 +12,7 @@ module Option = {
};
};

module O: MonadThing = Option;
module O = Option;

module Result = {
let map /*: t 'a 'b => f::('a => 'c) => t 'c 'b*/ = (value, ~f as use) =>
Expand Down
Loading