Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Mar 26, 2023
0 parents commit 4fc0587
Show file tree
Hide file tree
Showing 1,676 changed files with 840,006 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/lib
tests/.bsb.lock
_build
tests/.merlin
resgraph.exe
_opam
.DS_Store
11 changes: 11 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
profile = default
version = 0.22.4

field-space = tight-decl
break-cases = toplevel
module-item-spacing = preserve
cases-exp-indent = 2
space-around-arrays = false
space-around-lists = false
space-around-records = false
space-around-variants = false
2 changes: 2 additions & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/compiler-libs-406/*
vendor/res_outcome_printer/*
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ocaml.sandbox": {
"kind": "opam",
"switch": "4.14.0"
}
}
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
SHELL = /bin/bash

build-resgraph-binary:
rm -f resgraph.exe
dune build
cp _build/install/default/bin/resgraph resgraph.exe

build-tests:
make -C tests build

build: build-resgraph-binary build-tests

dce: build-resgraph-binary
opam exec reanalyze.exe -- -dce-cmt _build -suppress vendor

format:
dune build @fmt --auto-promote

test-resgraph-binary: build-resgraph-binary
make -C tests test

test: test-resgraph-binary

clean:
rm -f resgraph.exe
dune clean
make -C tests clean
make -C reanalyze clean

checkformat:
dune build @fmt

.DEFAULT_GOAL := build

.PHONY: build-resgraph-binary build-tests dce clean format test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ResGraph
16 changes: 16 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(dirs src reanalyze vendor)

(env
(dev
(env-vars
(CPPO_FLAGS -U=RELEASE)))
(release
(env-vars
(CPPO_FLAGS -D=RELEASE))
(ocamlopt_flags
(:standard -O3 -unbox-closures)))
(static
(env-vars
(CPPO_FLAGS -D=RELEASE))
(ocamlopt_flags
(:standard -O3 -unbox-closures))))
25 changes: 25 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(lang dune 2.3)

(generate_opam_files true)

(authors "Gabriel Nordeborn")

(maintainers "Gabriel Nordeborn")

(homepage "https://github.com/rescript-lang/rescript-vscode")

(bug_reports "https://github.com/rescript-lang/rescript-vscode/issues")

(package
(name resgraph)
(synopsis "ResGraph")
(depends
(ocaml
(>= 4.10))
(ocamlformat
(= 0.22.4))
(cppo
(= 1.6.9))
(reanalyze
(= 2.23.0))
dune))
Binary file added rescript-editor-analysis.exe
Binary file not shown.
28 changes: 28 additions & 0 deletions resgraph.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "ResGraph"
maintainer: ["Gabriel Nordeborn"]
authors: ["Gabriel Nordeborn"]
homepage: "https://github.com/rescript-lang/rescript-vscode"
bug-reports: "https://github.com/rescript-lang/rescript-vscode/issues"
depends: [
"ocaml" {>= "4.10"}
"ocamlformat" {= "0.22.4"}
"cppo" {= "1.6.9"}
"reanalyze" {= "2.23.0"}
"dune"
]
build: [
["dune" "subst"] {pinned}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
24 changes: 24 additions & 0 deletions src/BuildSystem.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let namespacedName namespace name =
match namespace with
| None -> name
| Some namespace -> name ^ "-" ^ namespace

let ( /+ ) = Filename.concat

let getBsPlatformDir rootPath =
let result =
ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript"
in
match result with
| Some path -> Some path
| None ->
let message = "rescript could not be found" in
Log.log message;
None

let getLibBs root = Files.ifExists (root /+ "lib" /+ "bs")

let getStdlib base =
match getBsPlatformDir base with
| None -> None
| Some bsPlatformDir -> Some (bsPlatformDir /+ "lib" /+ "ocaml")
3 changes: 3 additions & 0 deletions src/Cfg.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let supportsSnippets = ref false

let debugFollowCtxPath = ref false
99 changes: 99 additions & 0 deletions src/Cli.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
let help =
{|
**Private CLI For rescript-vscode usage only**

API examples:
./resgraph.exe completion src/MyFile.res 0 4 currentContent.res true
./resgraph.exe definition src/MyFile.res 9 3
./resgraph.exe typeDefinition src/MyFile.res 9 3
./resgraph.exe documentSymbol src/Foo.res
./resgraph.exe hover src/MyFile.res 10 2 true
./resgraph.exe references src/MyFile.res 10 2
./resgraph.exe rename src/MyFile.res 10 2 foo
./resgraph.exe diagnosticSyntax src/MyFile.res
./resgraph.exe inlayHint src/MyFile.res 0 3 25
./resgraph.exe codeLens src/MyFile.res

Dev-time examples:
./resgraph.exe dump src/MyFile.res src/MyFile2.res
./resgraph.exe test src/MyFile.res

Note: positions are zero-indexed (start at 0 0), following LSP.
https://microsoft.github.io/language-server-protocol/specification#position

Options:
completion: compute autocomplete for MyFile.res at line 0 and column 4,
where MyFile.res is being edited and the editor content is in file current.res.

./resgraph.exe completion src/MyFile.res 0 4 current.res

definition: get definition for item in MyFile.res at line 10 column 2:

./resgraph.exe definition src/MyFile.res 10 2

typeDefinition: get type definition for item in MyFile.res at line 10 column 2:

./resgraph.exe typeDefinition src/MyFile.res 10 2

documentSymbol: get all symbols declared in MyFile.res

./resgraph.exe documentSymbol src/MyFile.res

hover: get inferred type for MyFile.res at line 10 column 2 (supporting markdown links):

./resgraph.exe hover src/MyFile.res 10 2 true

references: get all references to item in MyFile.res at line 10 column 2:

./resgraph.exe references src/MyFile.res 10 2

rename: rename all appearances of item in MyFile.res at line 10 column 2 with foo:

./resgraph.exe rename src/MyFile.res 10 2 foo

semanticTokens: return token semantic highlighting info for MyFile.res

./resgraph.exe semanticTokens src/MyFile.res

createInterface: print to stdout the interface file for src/MyFile.res

./resgraph.exe createInterface src/MyFile.res lib/bs/src/MyFile.cmi

format: print to stdout the formatted version of the provided file

./resgraph.exe format src/MyFile.res

diagnosticSyntax: print to stdout diagnostic for syntax

./resgraph.exe diagnosticSyntax src/MyFile.res

inlayHint: get all inlay Hint between line 0 and 3 declared in MyFile.res. Last argument is maximum of character length for inlay hints

./resgraph.exe inlayHint src/MyFile.res 0 3 25

codeLens: get all code lens entries for file src/MyFile.res

./resgraph.exe codeLens src/MyFile.res

signatureHelp: get signature help if available for position at line 10 column 2 in src/MyFile.res

./resgraph.exe signatureHelp src/MyFile.res 10 2

test: run tests specified by special comments in file src/MyFile.res

./resgraph.exe test src/src/MyFile.res
|}

let main () =
match Array.to_list Sys.argv with
| [_; "generate-schema"; path] -> GenerateSchema.generateSchema ~path ~debug:false
| [_; "test"; path] ->
Cfg.supportsSnippets := true;
Commands.test ~path
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help
| _ ->
prerr_endline help;
exit 1
;;

main ()
36 changes: 36 additions & 0 deletions src/Cmt.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
open SharedTypes

let fullForCmt ~moduleName ~package ~uri cmt =
match Shared.tryReadCmt cmt with
| None -> None
| Some infos ->
let file = ProcessCmt.fileForCmtInfos ~moduleName ~uri infos in
let extra = ProcessExtra.getExtra ~file ~infos in
Some {file; extra; package}

let fullFromUri ~uri =
let path = Uri.toPath uri in
match Packages.getPackage ~uri with
| None -> None
| Some package -> (
let moduleName =
BuildSystem.namespacedName package.namespace (FindFiles.getName path)
in
match Hashtbl.find_opt package.pathsForModule moduleName with
| Some paths ->
let cmt = getCmtPath ~uri paths in
fullForCmt ~moduleName ~package ~uri cmt
| None ->
prerr_endline ("can't find module " ^ moduleName);
None)

let fullsFromModule ~package ~moduleName =
if Hashtbl.mem package.pathsForModule moduleName then
let paths = Hashtbl.find package.pathsForModule moduleName in
let uris = getUris paths in
uris |> List.filter_map (fun uri -> fullFromUri ~uri)
else []

let loadFullCmtFromPath ~path =
let uri = Uri.fromPath path in
fullFromUri ~uri
54 changes: 54 additions & 0 deletions src/Commands.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
let test ~path =
Uri.stripPath := true;
match Files.readFile path with
| None -> assert false
| Some text ->
let lines = text |> String.split_on_char '\n' in
let processLine i line =
let createCurrentFile () =
let currentFile, cout = Filename.open_temp_file "def" "txt" in
let removeLineComment l =
let len = String.length l in
let rec loop i =
if i + 2 <= len && l.[i] = '/' && l.[i + 1] = '/' then Some (i + 2)
else if i + 2 < len && l.[i] = ' ' then loop (i + 1)
else None
in
match loop 0 with
| None -> l
| Some indexAfterComment ->
String.make indexAfterComment ' '
^ String.sub l indexAfterComment (len - indexAfterComment)
in
lines
|> List.iteri (fun j l ->
let lineToOutput =
if j == i - 1 then removeLineComment l else l
in
Printf.fprintf cout "%s\n" lineToOutput);
close_out cout;
currentFile
in
if Str.string_match (Str.regexp "^ *//[ ]*\\^") line 0 then
let matched = Str.matched_string line in
let len = line |> String.length in
let mlen = String.length matched in
let rest = String.sub line mlen (len - mlen) in
let line = i - 1 in
let col = mlen - 1 in
if mlen >= 3 then (
(match String.sub rest 0 3 with
| "db+" -> Log.verbose := true
| "db-" -> Log.verbose := false
| "gen" -> GenerateSchema.generateSchema ~path ~debug:true
| "ast" ->
print_endline
("Dump AST " ^ path ^ " " ^ string_of_int line ^ ":"
^ string_of_int col);
let currentFile = createCurrentFile () in
DumpAst.dump ~pos:(line, col) ~currentFile;
Sys.remove currentFile
| _ -> ());
print_newline ())
in
lines |> List.iteri processLine
Loading

0 comments on commit 4fc0587

Please sign in to comment.