Permalink
Browse files

improved detection of case statements for autocomplete

Summary:
refs #8241

There's a bunch of cases for autocomplete after colons:

```
case 'foo':AUTO332 // should not autocomplete
case 'foo':namespace\AUTO332 // should autocomplete
case Foo::AUTO332 // should autocomplete
case AUTO332 // should autocomplete
return <x:AUTO332 // should autocomplete
```

This diff:
- differrentiates the first two cases when in a namespace by looking at the id length instead of the string, as the current namespace is included in the search string
- handles string literal case statements

Reviewed By: pittsw

Differential Revision: D8884007

fbshipit-source-id: 90eec46af0ff164e6bb9ea961e4fc48e51744d14
  • Loading branch information...
fredemmott committed Jul 18, 2018
1 parent 65b0276 commit ff979c963a8cf6c507233cae176b279a05f74380
@@ -707,14 +707,17 @@ let handle_mode
match mode with
| Ai _ -> ()
| Autocomplete ->
let token = "AUTO332" in
let token_len = String.length token in
let file = cat (Relative_path.to_absolute filename) in
let autocomplete_context = { AutocompleteTypes.
is_xhp_classname = false;
is_instance_member = false;
is_after_single_colon = false;
} in (* feature not implemented here; it only works for LSP *)
let result = ServerAutoComplete.auto_complete
~tcopt ~delimit_on_namespaces:false ~autocomplete_context file in
(* Search backwards: there should only be one /real/ case. If there's multiple, *)
(* guess that the others are preceding explanation comments *)
let offset = Str.search_backward (Str.regexp token) file (String.length file) in
let pos = File_content.offset_to_position file offset in
let file = (Str.string_before file offset) ^ (Str.string_after file (offset + token_len)) in
let result = ServerAutoComplete.auto_complete_at_position
~tcopt ~pos ~delimit_on_namespaces:false ~file_content:file in
List.iter ~f: begin fun r ->
let open AutocompleteTypes in
Printf.printf "%s %s\n" r.res_name r.res_ty
@@ -239,7 +239,12 @@ let compute_complete_global
(* Colon hack: in "case Foo::Bar:", we wouldn't want to show autocomplete *)
(* here, but we would in "<nt:" and "$a->:" and "function f():". We can *)
(* recognize this case by whether the prefix is empty. *)
if autocomplete_context.is_after_single_colon && gname = "" then
(* We do this by checking the identifier length, as the string will *)
(* include the current namespace. *)
let have_user_prefix = match !autocomplete_identifier with
| None -> failwith "No autocomplete position was set"
| Some (pos, _) -> Pos.length pos > suffix_len in
if autocomplete_context.is_after_single_colon && not have_user_prefix then
()
else begin
@@ -43,7 +43,8 @@ let auto_complete
let context_xhp_classname_regex = Str.regexp ".*<[a-zA-Z_0-9:]*$"
let context_xhp_member_regex = Str.regexp ".*->[a-zA-Z_0-9:]*$"
let context_after_single_colon_regex = Str.regexp ".*[a-zA-Z_0-9]:$"
(* For identifying case statements from text context *)
let context_after_single_colon_regex = Str.regexp ".*[a-zA-Z_0-9\"']:$"
let get_autocomplete_context
(content:string)
@@ -0,0 +1,13 @@
<?hh
// Namespace is important: we need to check that there's no user-supplied
// prefix, not just that there is no prefix at all, and AUTO332 expands to
// Foo\AUTO322
namespace Foo;
function main(): void {
switch(\random_int(123, 456)) {
case 123:AUTO332
}
}
No changes.
@@ -0,0 +1,15 @@
<?hh
// Namespace is important: we need to check that there's no user-supplied
// prefix, not just that there is no prefix at all, and AUTO332 expands to
// Foo\AUTO322
namespace Foo;
class ABC {}
function main(): void {
switch(\random_int(123, 456)) {
case 123:namespace\AUTO332
}
}
@@ -0,0 +1,2 @@
Foo\main (function(): void)
Foo\ABC class
@@ -0,0 +1,15 @@
<?hh
// Namespace is important: we need to check that there's no user-supplied
// prefix, not just that there is no prefix at all, and AUTO332 expands to
// Foo\AUTO322
namespace Foo;
class ABC {}
function main(): void {
switch(\random_int(123, 456)) {
case 123:AAUTO332
}
}
@@ -0,0 +1 @@
Foo\ABC class
@@ -0,0 +1,7 @@
<?hh
class :foo:bar {}
function main(): void {
$x = <foo:AUTO332
}
@@ -0,0 +1 @@
foo:bar class

0 comments on commit ff979c9

Please sign in to comment.