Browse Source

[parser] fix `package` showing up in completion

see #7053
Simon Krajewski 7 years ago
parent
commit
c08f9417bc

+ 6 - 3
src/compiler/displayOutput.ml

@@ -657,10 +657,13 @@ let handle_syntax_completion com kind p =
 			[Extends]
 		| SCComment ->
 			[]
-		| SCTypeDecl(had_package,had_non_import) ->
+		| SCTypeDecl mode ->
 			let l = [Private;Extern;Class;Interface;Enum;Abstract;Typedef;Final] in
-			let l = if had_package then l else Package :: l in
-			let l = if had_non_import then l else Import :: Using :: l in
+			let l = match mode with
+				| TCBeforePackage -> Package :: Import :: Using :: l
+				| TCAfterImport -> Import :: Using :: l
+				| TCAfterType -> l
+			in
 			l
 		| SCAfterTypeFlag flags ->
 			let l = [Class;Interface] in

+ 5 - 5
src/syntax/grammar.mly

@@ -139,12 +139,12 @@ and parse_type_decls pmax pack acc s =
 			(* print_endline (Printf.sprintf "(%i <= %i) (%i > %i)" pmax !display_position.pmin pmin !display_position.pmax); *)
 			if pmax <= !display_position.pmin && pmin > !display_position.pmax then begin
 				let had_package = pack <> [] in
-				let had_non_import = match acc with
-					| [] -> false
-					| ((EClass _ | EEnum _ | ETypedef _ | EAbstract _),_) :: _ -> true
-					| ((EImport _ | EUsing _),_) :: _ -> false
+				let mode = match acc with
+					| [] -> if had_package then TCAfterImport else TCBeforePackage
+					| ((EClass _ | EEnum _ | ETypedef _ | EAbstract _),_) :: _ -> TCAfterType
+					| ((EImport _ | EUsing _),_) :: _ -> TCAfterImport
 				in
-				delay_syntax_completion (SCTypeDecl(had_package,had_non_import)) !display_position
+				delay_syntax_completion (SCTypeDecl mode) !display_position
 			end
 		end;
 		match s with parser

+ 6 - 1
src/syntax/parser.ml

@@ -37,11 +37,16 @@ type decl_flag =
 	| DExtern
 	| DFinal
 
+type type_decl_completion_mode =
+	| TCBeforePackage
+	| TCAfterImport
+	| TCAfterType
+
 type syntax_completion =
 	| SCComment
 	| SCClassRelation
 	| SCInterfaceRelation
-	| SCTypeDecl of bool (* had package *) * bool (* had non-import/using *)
+	| SCTypeDecl of type_decl_completion_mode
 	| SCAfterTypeFlag of decl_flag list
 
 exception Error of error_msg * pos

+ 4 - 1
tests/display/src/cases/Issue7053.hx

@@ -22,6 +22,9 @@ class Issue7053 extends DisplayTestCase {
 		for (expected in ["import", "using", "private", "extern", "class", "interface", "enum", "abstract", "typedef", "final"]) {
 			eq(true, hasField(fields, expected, null, "keyword"));
 		}
+		for (unexpected in ["package"]) {
+			eq(false, hasField(fields, unexpected, null, "keyword"));
+		}
 	}
 
 	/**
@@ -33,7 +36,7 @@ class Issue7053 extends DisplayTestCase {
 		for (expected in ["private", "extern", "class", "interface", "enum", "abstract", "typedef", "final"]) {
 			eq(true, hasField(fields, expected, null, "keyword"));
 		}
-		for (unexpected in ["import", "using"]) {
+		for (unexpected in ["import", "using", "package"]) {
 			eq(false, hasField(fields, unexpected, null, "keyword"));
 		}
 	}