Principled Syntactic Code Completion with Placeholders

64
Principled Syntactic Code Completion using Placeholders SLE’16 - Amsterdam Sebastian Erdweg, Guido Wachsmuth, Eelco Visser Eduardo Souza

Transcript of Principled Syntactic Code Completion with Placeholders

Principled Syntactic Code Completion using Placeholders

SLE’16 - Amsterdam

Sebastian Erdweg, Guido Wachsmuth, Eelco VisserEduardo Souza

2

3

*S. Amann, S. Proksch, S. Nadi, and M. Mezini. A study of visual studio usage in practice. In SANER, 2016.

4

Syntactic Completion

Semantic Completion

5

Syntactic Completion

Semantic Completion

6

Problems

7

Problems

Unsound!

8

Soundness: the resulting program should have no additional syntax errors.

Problems

9

Problems

Unsound!

10

Incomplete

!

Completeness: any program should be derivable through code completion.

Problems

Unsound!

11

Problems

Incomplete

!Unso

und!

12

Ad-hoc!

Problems

Incomplete

!Unso

und!

13

Language Specification

Syntax Definition

Name Binding

Type Constraints

Code Completion

Syntactic Completion

Semantic Completion

Textual/Projectional IDE

Language Workbenches

14

15

Unsound!

16

Sound and Complete Syntactic Code Completion from Language

Specifications

17

Language Specification

SyntaxDefinition

Name Binding

Type Constraints

Code Completion

SyntacticCompletion

Semantic Completion

Textual IDE

Sound!

Complete!

Language Workbenches

18

Language Specification

SDF3 Name Binding

Type Constraints

Code Completion

SyntacticCompletion

Semantic Completion

Textual IDE

Sound!

Complete!

19

Explicit Placeholders

20

Incompleteprograms

21

Make incompleteness valid using placeholders!

Incompleteprograms

22

23

24

25

26

4

2

3

1

27

Deriving Syntactic Completion from the Syntax Definition

context-free syntax

Statement ::= "if" "(" Exp ")" Statement "else" Statement {"If"}

28

context-free syntax // placeholder rule

Statement ::= “$Statement" {“Statement-Plhdr"}

context-free syntax //regular production rule

Statement ::= "if" "(" Exp ")" Statement "else" Statement {"If"}

Make placeholders part of the language

Stm-Plhdr

If

Exp-Plhdr Stm-Plhdr

29

Stm-Plhdr

context-free syntax //regular production rule

Statement ::= "if" "(" Exp ")" Statement "else" Statement {"If"}

signature constructors // AST signatures

If : Exp * Statement * Statement -> Statement

Calculate placeholder expansions

30

rules rewrite-placeholder: Statement-Plhdr() -> If(Exp-Plhdr(), Statement-Plhdr(), Statement-Plhdr())

Format Expansions

context-free syntax

Statement ::= "if" "(" Exp ")" Statement "else" Statement {cons("If")}

context-free syntax // placeholder rule

Statement ::= "$Statement"Parse Placeholders

Expand Placeholders

Incompleteprograms

Completeprograms

Expand placeholder

Expand/overwrite placeholders

Correctprograms

31

32

Completeprograms

33

How to expand?

Completeprograms

34

Type a placeholder?

35

Type a placeholder?

36

Type a placeholder?

37

Inferring Placeholders

Optional Parent

List of Statements

38

Add Element to List

Add Optional Element

Placeholder Inference

Optional Parent

List of Statements

Add Optional Element

39

No source region

Add first or last element

Placeholder Inference

Add Element to List

ClassDecl

class “A” {NoParent ConsMethod

[…]

}NilField

40

Placeholder Inference - Optional

41

ClassDecl

class “A” {NoParent ConsMethod

[…]

}NilField

Placeholder Inference - Optional

42

ClassDecl

class “A” {NoParent ConsMethod

[…]

}NilField

Placeholder Inference - Optional

43

Placeholder Inference - Optional

44

Placeholder Inference - Lists

[…]

[…]

Method

Cons

VarDecl

AssignInt “x”

VarRef

“x”

Add

Int

21

Cons

Nil

{ return[…]

int ;

Int

=

21

45

Placeholder Inference - Lists

[…]

[…]

Method

Cons

VarDecl

AssignInt “x”

VarRef

“x”

Add

Int

21

Cons

Nil

{ return[…]

int ;

Int

=

21

46

Placeholder Inference - Lists

[…]

[…]

Method

Cons

VarDecl

AssignInt “x”

VarRef

“x”

Add

Int

21

Cons

Nil

{ return[…]

int ;

Int

=

21

47

Placeholder Inference - Lists

[…]

[…]

Method

Cons

VarDecl

AssignInt “x”

VarRef

“x”

Add

Int

21

Cons

Nil

{ return[…]

int ;

Int

=

21

48

49

Incompleteprograms

Completeprograms

Expand placeholder

Infer placeholder

Correctprograms

Infer placeholder

Expand/overwrite placeholders

50

Incorrectprograms

51

Error Recovery

52

53

54

context-free syntax //regular syntax rules

Statement.VarDecl = <<Type> <ID>;> Statement.Assign = <<VarRef> = <Exp>;>

Insertion Rules

// derived insertion rules for placeholderscontext-free syntax

Type.Type-Plhdr = {symbol-insertion} ID.ID-Plhdr = {symbol-insertion} Statement.Statement-Plhdr = {symbol-insertion} VarRef.VarRef-Plhdr = {symbol-insertion} Exp.Exp-Plhdr = {symbol-insertion}

// derived insertion rules for literalslexical syntax

"=" = {symbol-completion} ";" = {symbol-completion}

55

Proposal nodes

Insertion nodes

[…]

[…]

Stmt*

amb

VarDecl

ClassType

“x”

VarDecl

[…]

Assign

ID-Plhdr ;

Assign

VarRef

“x”

Exp-Plhdr ;=

Apply insertion rules at the cursor

56

Limit the search space!

Exp-Plhdr Exp-Plhdr+

Assign

VarRef

“x”

;=

[…]

Add

Assign

VarRef

“x”

;=

[…]

Exp-Plhdr

57

Greedy recovery!

[…]

[…]

Stmt*

amb

VarDecl

ClassType

“x”

VarDecl

[…]

Assign

ID-Plhdr ;

Assign

VarRef

“x”

Exp-Plhdr ;=

58

Nested Proposal Nodes

IntValue Exp-Plhdr+

Assign

VarRef

“x”

;=

[…]

Add

1

59

Incorrectprograms

Incompleteprograms

Completeprograms

Correctprograms

60

Incorrectprograms

Incompleteprograms

Completeprograms

Expand placeholder

Correctprograms

Expand/overwrite placeholders

61

Incorrectprograms

Incompleteprograms

Completeprograms

Expand placeholder

Infer placeholder

Correctprograms

Infer placeholder

Expand/overwrite placeholders

62

Incorrectprograms

Incompleteprograms

Completeprograms

Expand placeholder

Infer placeholder

Correctprograms

Infer placeholder

Expand/overwrite placeholders

Recover incomplete

program

Recover completeprogram

63

64

Language Specification

SDF3 Name Binding

Type Constraints

Code Completion

SyntacticCompletion

Semantic Completion

Sound!

Complete!

Textual IDE