From 626007086f0a37324a9c0802766458e7c5769424 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 2 Aug 2022 18:27:57 +0200 Subject: [PATCH 1/4] consolidate some of the logic around checking if a file is a res file or not --- analysis/src/Files.ml | 2 ++ analysis/src/Hint.ml | 4 ++-- analysis/src/SemanticTokens.ml | 2 +- analysis/src/Xform.ml | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/analysis/src/Files.ml b/analysis/src/Files.ml index 5da85fd20..1bee89ac5 100644 --- a/analysis/src/Files.ml +++ b/analysis/src/Files.ml @@ -95,3 +95,5 @@ let rec collect ?(checkDir = fun _ -> true) path test = |> List.concat else [] | _ -> if test path then [path] else [] + +let isResFile path = exists path && Filename.check_suffix path ".res" diff --git a/analysis/src/Hint.ml b/analysis/src/Hint.ml index 1f69cf93a..42ea8b40c 100644 --- a/analysis/src/Hint.ml +++ b/analysis/src/Hint.ml @@ -82,7 +82,7 @@ let inlay ~path ~pos ~maxLength ~debug = Ast_iterator.default_iterator.value_binding iterator vb in let iterator = {Ast_iterator.default_iterator with value_binding} in - (if Files.exists path && Filename.check_suffix path ".res" then + (if Files.isResFile path then let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in @@ -143,7 +143,7 @@ let codeLens ~path ~debug = let iterator = {Ast_iterator.default_iterator with value_binding} in (* We only print code lenses in implementation files. This is because they'd be redundant in interface files, where the definition itself will be the same thing as what would've been printed in the code lens. *) - (if Files.exists path && Filename.check_suffix path ".res" then + (if Files.isResFile path then let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in diff --git a/analysis/src/SemanticTokens.ml b/analysis/src/SemanticTokens.ml index 80a1badb5..de24f9a07 100644 --- a/analysis/src/SemanticTokens.ml +++ b/analysis/src/SemanticTokens.ml @@ -431,7 +431,7 @@ let command ~debug ~emitter ~path = } in - if Filename.check_suffix path ".res" then ( + if Files.isResFile path then ( let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in diff --git a/analysis/src/Xform.ml b/analysis/src/Xform.ml index a402ca92e..16a2bd8d5 100644 --- a/analysis/src/Xform.ml +++ b/analysis/src/Xform.ml @@ -302,7 +302,7 @@ let parse ~filename = let extractCodeActions ~path ~pos ~currentFile ~debug = match Cmt.fullFromPath ~path with - | Some full when Filename.check_suffix currentFile ".res" -> + | Some full when Files.isResFile currentFile -> let structure, printExpr, printStructureItem = parse ~filename:currentFile in From a9ed2037b36813751c833b795296dafeace5f217 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Tue, 2 Aug 2022 18:29:42 +0200 Subject: [PATCH 2/4] flip logic so we only check for file existance if it looks like a valid file --- analysis/src/Files.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analysis/src/Files.ml b/analysis/src/Files.ml index 1bee89ac5..ad4abd89c 100644 --- a/analysis/src/Files.ml +++ b/analysis/src/Files.ml @@ -96,4 +96,4 @@ let rec collect ?(checkDir = fun _ -> true) path test = else [] | _ -> if test path then [path] else [] -let isResFile path = exists path && Filename.check_suffix path ".res" +let isResFile path = Filename.check_suffix path ".res" && exists path From 8393a54a4ec2aa5daa59ef7bfa13594ccb89f6c8 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 3 Aug 2022 19:36:50 +0200 Subject: [PATCH 3/4] change classification strategy to something harder to accidentally skip --- analysis/src/Files.ml | 7 ++++++- analysis/src/Hint.ml | 4 ++-- analysis/src/SemanticTokens.ml | 2 +- analysis/src/Xform.ml | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/analysis/src/Files.ml b/analysis/src/Files.ml index ad4abd89c..ecb96885e 100644 --- a/analysis/src/Files.ml +++ b/analysis/src/Files.ml @@ -96,4 +96,9 @@ let rec collect ?(checkDir = fun _ -> true) path test = else [] | _ -> if test path then [path] else [] -let isResFile path = Filename.check_suffix path ".res" && exists path +type classifiedFile = Res | Resi | Other + +let classifyFile path = + if Filename.check_suffix path ".res" && exists path then Res + else if Filename.check_suffix path ".resi" && exists path then Resi + else Other diff --git a/analysis/src/Hint.ml b/analysis/src/Hint.ml index 42ea8b40c..47f23e241 100644 --- a/analysis/src/Hint.ml +++ b/analysis/src/Hint.ml @@ -82,7 +82,7 @@ let inlay ~path ~pos ~maxLength ~debug = Ast_iterator.default_iterator.value_binding iterator vb in let iterator = {Ast_iterator.default_iterator with value_binding} in - (if Files.isResFile path then + (if Files.classifyFile path = Res then let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in @@ -143,7 +143,7 @@ let codeLens ~path ~debug = let iterator = {Ast_iterator.default_iterator with value_binding} in (* We only print code lenses in implementation files. This is because they'd be redundant in interface files, where the definition itself will be the same thing as what would've been printed in the code lens. *) - (if Files.isResFile path then + (if Files.classifyFile path = Res then let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in diff --git a/analysis/src/SemanticTokens.ml b/analysis/src/SemanticTokens.ml index de24f9a07..85102bfd2 100644 --- a/analysis/src/SemanticTokens.ml +++ b/analysis/src/SemanticTokens.ml @@ -431,7 +431,7 @@ let command ~debug ~emitter ~path = } in - if Files.isResFile path then ( + if Files.classifyFile path = Res then ( let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in diff --git a/analysis/src/Xform.ml b/analysis/src/Xform.ml index 16a2bd8d5..9859bf64c 100644 --- a/analysis/src/Xform.ml +++ b/analysis/src/Xform.ml @@ -302,7 +302,7 @@ let parse ~filename = let extractCodeActions ~path ~pos ~currentFile ~debug = match Cmt.fullFromPath ~path with - | Some full when Files.isResFile currentFile -> + | Some full when Files.classifyFile currentFile = Res -> let structure, printExpr, printStructureItem = parse ~filename:currentFile in From 0ec6cdcac5867ae93cb9eca665513c5aaf5fa11f Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 3 Aug 2022 20:56:58 +0200 Subject: [PATCH 4/4] change fn name --- analysis/src/Files.ml | 2 +- analysis/src/Hint.ml | 4 ++-- analysis/src/SemanticTokens.ml | 8 +++++--- analysis/src/Xform.ml | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/analysis/src/Files.ml b/analysis/src/Files.ml index ecb96885e..9ab016da5 100644 --- a/analysis/src/Files.ml +++ b/analysis/src/Files.ml @@ -98,7 +98,7 @@ let rec collect ?(checkDir = fun _ -> true) path test = type classifiedFile = Res | Resi | Other -let classifyFile path = +let classifySourceFile path = if Filename.check_suffix path ".res" && exists path then Res else if Filename.check_suffix path ".resi" && exists path then Resi else Other diff --git a/analysis/src/Hint.ml b/analysis/src/Hint.ml index 47f23e241..4a258fe39 100644 --- a/analysis/src/Hint.ml +++ b/analysis/src/Hint.ml @@ -82,7 +82,7 @@ let inlay ~path ~pos ~maxLength ~debug = Ast_iterator.default_iterator.value_binding iterator vb in let iterator = {Ast_iterator.default_iterator with value_binding} in - (if Files.classifyFile path = Res then + (if Files.classifySourceFile path = Res then let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in @@ -143,7 +143,7 @@ let codeLens ~path ~debug = let iterator = {Ast_iterator.default_iterator with value_binding} in (* We only print code lenses in implementation files. This is because they'd be redundant in interface files, where the definition itself will be the same thing as what would've been printed in the code lens. *) - (if Files.classifyFile path = Res then + (if Files.classifySourceFile path = Res then let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in diff --git a/analysis/src/SemanticTokens.ml b/analysis/src/SemanticTokens.ml index 85102bfd2..1e7b54160 100644 --- a/analysis/src/SemanticTokens.ml +++ b/analysis/src/SemanticTokens.ml @@ -398,7 +398,8 @@ let command ~debug ~emitter ~path = let structure_item (iterator : Ast_iterator.iterator) (item : Parsetree.structure_item) = (match item.pstr_desc with - | Pstr_primitive {pval_name = {txt = id; loc}} -> emitter |> emitVariable ~id ~debug ~loc; + | Pstr_primitive {pval_name = {txt = id; loc}} -> + emitter |> emitVariable ~id ~debug ~loc | _ -> ()); Ast_iterator.default_iterator.structure_item iterator item in @@ -406,7 +407,8 @@ let command ~debug ~emitter ~path = let signature_item (iterator : Ast_iterator.iterator) (item : Parsetree.signature_item) = (match item.psig_desc with - | Psig_value {pval_name = {txt = id; loc}} -> emitter |> emitVariable ~id ~debug ~loc; + | Psig_value {pval_name = {txt = id; loc}} -> + emitter |> emitVariable ~id ~debug ~loc | _ -> ()); Ast_iterator.default_iterator.signature_item iterator item in @@ -431,7 +433,7 @@ let command ~debug ~emitter ~path = } in - if Files.classifyFile path = Res then ( + if Files.classifySourceFile path = Res then ( let parser = Res_driver.parsingEngine.parseImplementation ~forPrinter:false in diff --git a/analysis/src/Xform.ml b/analysis/src/Xform.ml index 9859bf64c..fe9e58f0f 100644 --- a/analysis/src/Xform.ml +++ b/analysis/src/Xform.ml @@ -302,7 +302,7 @@ let parse ~filename = let extractCodeActions ~path ~pos ~currentFile ~debug = match Cmt.fullFromPath ~path with - | Some full when Files.classifyFile currentFile = Res -> + | Some full when Files.classifySourceFile currentFile = Res -> let structure, printExpr, printStructureItem = parse ~filename:currentFile in