Skip to content

Commit

Permalink
noop: extract Full_fidelity_parser.sub_of_pos
Browse files Browse the repository at this point in the history
Summary: I wrote roughly the same function 3 times: might be a good time to factor it out, as suggested by ljw1004

Reviewed By: ljw1004

Differential Revision: D45912815

fbshipit-source-id: 866ac8555167d4707347890594665595cecb9b9f
  • Loading branch information
mheiber authored and facebook-github-bot committed May 17, 2023
1 parent 3614df0 commit c0ad080
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 33 deletions.
12 changes: 12 additions & 0 deletions hphp/hack/src/parser/full_fidelity_source_text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,15 @@ let relative_pos pos_file source_text start_offset end_offset =
let pos_start = offset_to_lnum_bol_offset start_offset in
let pos_end = offset_to_lnum_bol_offset end_offset in
Pos.make_from_lnum_bol_offset ~pos_file ~pos_start ~pos_end

let sub_of_pos ?length source_text pos =
let offset =
let (first_line, first_col) = Pos.line_column pos in
position_to_offset source_text (first_line, first_col + 1)
in
let length =
match length with
| Some n -> n
| None -> Pos.length pos
in
sub source_text offset length
3 changes: 3 additions & 0 deletions hphp/hack/src/parser/full_fidelity_source_text.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ val line_text : t -> int -> string
(** get a substring start at the ith char and continuing for length *)
val sub : t -> int -> int -> string

(** get a substring corresponding to a position. [length] defaults to the length of the position. *)
val sub_of_pos : ?length:int -> t -> Pos.t -> string

(** convert an absolute offset into a (line number, column) pair *)
val offset_to_position : t -> int -> int * int

Expand Down
15 changes: 2 additions & 13 deletions hphp/hack/src/server/codeActionsServiceExtractVariable.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ type candidate = {
placeholder_n: int;
}

let source_slice ~source_text ~start_pos ~length =
let offset =
let (first_line, first_col) = Pos.line_column start_pos in
Full_fidelity_source_text.position_to_offset
source_text
(first_line, first_col + 1)
in
Full_fidelity_source_text.sub source_text offset length

(**
We don't want to extract variables for lambdas like this: `() ==> 200`.
The AST of such a lambda is indistinguishable from `() ==> { return 200; }`
Expand All @@ -35,7 +26,7 @@ let might_be_expression_lambda ~f_body:Aast.{ fb_ast } ~pos ~source_text =
| [(stmt_pos, _)] ->
let length = Pos.start_offset stmt_pos - Pos.start_offset pos in
if length > 0 then
let src = source_slice ~source_text ~start_pos:pos ~length in
let src = Full_fidelity_source_text.sub_of_pos source_text pos ~length in
not @@ String.is_substring ~substring:"{" src
else
(* length can be negative to curlies in default params: `(($a = () ==> {}) ==> ...` *)
Expand Down Expand Up @@ -158,9 +149,7 @@ let top_visitor (selection : Pos.t) ~source_text =
let command_or_action_of_candidate
~source_text ~path { stmt_pos; pos; placeholder_n } =
let placeholder = Format.sprintf "%s%d" placeholder_base placeholder_n in
let exp_string =
source_slice ~source_text ~start_pos:pos ~length:(Pos.length pos)
in
let exp_string = Full_fidelity_source_text.sub_of_pos source_text pos in
let change_expression =
Lsp.TextEdit.
{
Expand Down
11 changes: 1 addition & 10 deletions hphp/hack/src/server/codeActionsServiceFlipAroundComma.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ end = struct
{ insertion_index; positions; pos }
end

let source_slice ~source_text pos =
let offset =
let (first_line, first_col) = Pos.line_column pos in
Full_fidelity_source_text.position_to_offset
source_text
(first_line, first_col + 1)
in
Full_fidelity_source_text.sub source_text offset (Pos.length pos)

let list_flip ~insertion_index l =
let rec aux i = function
| h1 :: h2 :: tail when i = insertion_index - 1 -> h2 :: h1 :: tail
Expand Down Expand Up @@ -198,7 +189,7 @@ let edit_of_candidate
~path ~source_text Candidate.{ insertion_index; positions; pos } =
let text =
positions
|> List.map ~f:(source_slice ~source_text)
|> List.map ~f:(Full_fidelity_source_text.sub_of_pos source_text)
|> list_flip ~insertion_index
|> String.concat ~sep:", "
in
Expand Down
13 changes: 3 additions & 10 deletions hphp/hack/src/server/codeActionsServiceInlineVariable.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ type candidate = {
use_pos: Pos.t;
}

let source_slice ~source_text pos =
let offset =
let (first_line, first_col) = Pos.line_column pos in
Full_fidelity_source_text.position_to_offset
source_text
(first_line, first_col + 1)
in
Full_fidelity_source_text.sub source_text offset (Pos.length pos)

let remove_leading_whitespace ~source_text pos : Pos.t =
let rec calc_strip_amount offset =
let ch = Full_fidelity_source_text.get source_text offset in
Expand Down Expand Up @@ -280,7 +271,9 @@ let command_or_action_of_candidate ~path ~source_text { name; def; use_pos } =
Lsp.{ TextEdit.range; newText = "" }
in
let change_replace_use =
let text = source_slice ~source_text def.def_rhs_pos in
let text =
Full_fidelity_source_text.sub_of_pos source_text def.def_rhs_pos
in
let text =
if def.def_needs_grouping then
Printf.sprintf "(%s)" text
Expand Down

0 comments on commit c0ad080

Please sign in to comment.