mirror of
https://github.com/comby-tools/comby.git
synced 2024-08-16 08:40:55 +03:00
lines and offsets properties (#291)
This commit is contained in:
parent
e97f8663c5
commit
7a7a2a53e9
@ -343,6 +343,13 @@ module Matchers : sig
|
||||
type kind =
|
||||
| Value
|
||||
| Length
|
||||
| Lines
|
||||
| OffsetStart
|
||||
| OffsetEnd
|
||||
| LineStart
|
||||
| LineEnd
|
||||
| ColumnStart
|
||||
| ColumnEnd
|
||||
| LsifHover
|
||||
| FileName
|
||||
| FilePath
|
||||
|
@ -78,6 +78,16 @@ module Make (Metasyntax : Types.Metasyntax.S) = struct
|
||||
let attribute_to_kind = function
|
||||
| "value" -> Value
|
||||
| "length" -> Length
|
||||
| "lines" -> Lines
|
||||
| "offset"
|
||||
| "offset.start" -> OffsetStart
|
||||
| "offset.end" -> OffsetEnd
|
||||
| "line"
|
||||
| "line.start" -> LineStart
|
||||
| "line.end" -> LineEnd
|
||||
| "column"
|
||||
| "column.start" -> ColumnStart
|
||||
| "column.end" -> ColumnEnd
|
||||
| "lsif.hover" -> LsifHover
|
||||
| "file.path" -> FilePath
|
||||
| "file.name" -> FileName
|
||||
@ -96,6 +106,16 @@ module Make (Metasyntax : Types.Metasyntax.S) = struct
|
||||
char '.' *> choice
|
||||
[ string "value"
|
||||
; string "length"
|
||||
; string "lines"
|
||||
; string "offset.start"
|
||||
; string "offset.end"
|
||||
; string "offset"
|
||||
; string "line.start"
|
||||
; string "line.end"
|
||||
; string "line"
|
||||
; string "column.start"
|
||||
; string "column.end"
|
||||
; string "column"
|
||||
; string "lsif.hover"
|
||||
; string "file.path"
|
||||
; string "file.name"
|
||||
@ -192,6 +212,55 @@ module Make (Metasyntax : Types.Metasyntax.S) = struct
|
||||
match kind with
|
||||
| Value -> Environment.lookup env variable
|
||||
| Length -> Environment.lookup env variable >>| length_to_string
|
||||
| Lines ->
|
||||
Environment.lookup env variable
|
||||
>>| String.count ~f:(Char.(=) '\n')
|
||||
>>| (fun v -> if v = 0 then 1 else v)
|
||||
>>| Int.to_string
|
||||
|
||||
| OffsetStart ->
|
||||
Environment.lookup_range env variable
|
||||
>>| fun { match_start = { offset; _ }; _ } ->
|
||||
Int.to_string offset
|
||||
| OffsetEnd ->
|
||||
Environment.lookup_range env variable
|
||||
>>| fun { match_end = { offset; _ }; _ } ->
|
||||
Int.to_string offset
|
||||
|
||||
| LineStart ->
|
||||
filepath >>= fun filepath ->
|
||||
Environment.lookup_range env variable
|
||||
>>| fun { match_start = { offset; _ }; _ } ->
|
||||
let source = In_channel.read_all filepath in (* Inefficient. *)
|
||||
let index = Match.Offset.index ~source in
|
||||
let line, _ = Match.Offset.convert_fast ~offset index in
|
||||
Int.to_string line
|
||||
| LineEnd ->
|
||||
filepath >>= fun filepath ->
|
||||
Environment.lookup_range env variable
|
||||
>>| fun { match_end = { offset; _ }; _ } ->
|
||||
let source = In_channel.read_all filepath in (* Inefficient. *)
|
||||
let index = Match.Offset.index ~source in
|
||||
let line, _ = Match.Offset.convert_fast ~offset index in
|
||||
Int.to_string line
|
||||
|
||||
| ColumnStart ->
|
||||
filepath >>= fun filepath ->
|
||||
Environment.lookup_range env variable
|
||||
>>| fun { match_start = { offset; _ }; _ } ->
|
||||
let source = In_channel.read_all filepath in (* Inefficient. *)
|
||||
let index = Match.Offset.index ~source in
|
||||
let _, column = Match.Offset.convert_fast ~offset index in
|
||||
Int.to_string column
|
||||
| ColumnEnd ->
|
||||
filepath >>= fun filepath ->
|
||||
Environment.lookup_range env variable
|
||||
>>| fun { match_end = { offset; _ }; _ } ->
|
||||
let source = In_channel.read_all filepath in (* Inefficient. *)
|
||||
let index = Match.Offset.index ~source in
|
||||
let _, column = Match.Offset.convert_fast ~offset index in
|
||||
Int.to_string column
|
||||
|
||||
| LsifHover ->
|
||||
filepath >>= fun filepath ->
|
||||
if debug then Format.printf "File for lsif.hover lookup: %s@." filepath;
|
||||
|
@ -125,6 +125,13 @@ module Template = struct
|
||||
type kind =
|
||||
| Value
|
||||
| Length
|
||||
| Lines
|
||||
| OffsetStart
|
||||
| OffsetEnd
|
||||
| LineStart
|
||||
| LineEnd
|
||||
| ColumnStart
|
||||
| ColumnEnd
|
||||
| LsifHover
|
||||
| FileName
|
||||
| FilePath
|
||||
|
@ -70,3 +70,105 @@ let%expect_test "filepath_rule" =
|
||||
[%expect_exact {|ok: this/is/a/path|}];
|
||||
run (module Matchers.Omega.Generic) ~filepath source ~rule match_template "ok: :[x]";
|
||||
[%expect_exact {|ok: this/is/a/path|}]
|
||||
|
||||
let%expect_test "lines" =
|
||||
let source = {|
|
||||
{
|
||||
foo
|
||||
}
|
||||
{
|
||||
bar
|
||||
baz
|
||||
}
|
||||
|} in
|
||||
let match_template = "{:[x]}" in
|
||||
let rewrite_template = {|:[x].lines|}
|
||||
in
|
||||
run (module Matchers.Alpha.Generic) source match_template rewrite_template;
|
||||
[%expect_exact {|
|
||||
2
|
||||
3
|
||||
|}];
|
||||
run (module Matchers.Omega.Generic) source match_template rewrite_template;
|
||||
[%expect_exact {|
|
||||
2
|
||||
3
|
||||
|}]
|
||||
|
||||
let%expect_test "offsets" =
|
||||
let source = {|
|
||||
first
|
||||
< >things
|
||||
< >first
|
||||
|} in
|
||||
let match_template = ":[[x]]" in
|
||||
let rewrite_template = {|
|
||||
offset: :[[x]].offset
|
||||
offset.start: :[[x]].offset.start
|
||||
offset.end: :[[x]].offset.end
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|}
|
||||
in
|
||||
run (module Matchers.Alpha.Generic) source match_template rewrite_template;
|
||||
[%expect_exact {|
|
||||
|
||||
offset: 1
|
||||
offset.start: 1
|
||||
offset.end: 6
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|
||||
< >
|
||||
offset: 11
|
||||
offset.start: 11
|
||||
offset.end: 17
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|
||||
< >
|
||||
offset: 25
|
||||
offset.start: 25
|
||||
offset.end: 30
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|
||||
|}];
|
||||
run (module Matchers.Omega.Generic) source match_template rewrite_template;
|
||||
[%expect_exact {|
|
||||
|
||||
offset: 1
|
||||
offset.start: 1
|
||||
offset.end: 6
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|
||||
< >
|
||||
offset: 11
|
||||
offset.start: 11
|
||||
offset.end: 17
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|
||||
< >
|
||||
offset: 25
|
||||
offset.start: 25
|
||||
offset.end: 30
|
||||
line.start: :[[x]].line.start // can't compute without source yet
|
||||
line.end: :[[x]].line.end // can't compute without source yet
|
||||
column.start: :[[x]].column.start // can't compute without source yet
|
||||
column.end: :[[x]].column.end // can't compute without source yet
|
||||
|
||||
|}]
|
||||
|
Loading…
Reference in New Issue
Block a user