OLD | NEW |
---|---|
(Empty) | |
1 # Statement Completion | |
2 | |
3 ### Mission Statement | |
4 | |
5 The purpose of this feature is to add required syntax to the current | |
6 statement. The goal is to make the statement syntactically complete. | |
7 That is not possible to do in all cases. A best-effort attempt is made | |
8 when it cannot be done. | |
9 | |
10 ### Current Statement | |
11 | |
12 The term _statement completion_ comes from IntelliJ. It is also called | |
13 _smart enter_ there, which is a more general term. See the IntelliJ | |
14 [documentatopn.](https://www.jetbrains.com/help/idea/2017.1/auto-completing-code .html#statements_completion) | |
devoncarew
2017/05/09 13:15:06
sp
messick
2017/05/09 13:26:03
Done.
| |
15 | |
16 Rather than restricting the functionality to statements, in the sense of the gra mmar construct | |
17 called _statement_, it is best to think of code constructs. Statement completion | |
18 can be used to add syntax to declarations, statements, and some expressions. | |
19 Generally, the syntax additions are punctuation marks, such as semicolons, | |
20 parentheses, and braces. | |
21 | |
22 The _current statement_ then, is the code construct being written in the | |
23 editor, as identified by the position of the editing cursor. The _cursor_ is the | |
24 primary editing cursor of the active editor in IntelliJ. We ignore multiple | |
25 secondary cursors. | |
26 | |
27 If the _current statement_ is already syntactically complete then the feature | |
28 just adds a newline. This will be the case when the cursor follows the closing | |
29 brace of the body of a for-statement or while-statement, for example. The model | |
30 used is that the user is creating code going forward, and when the | |
31 _smart enter_ keystroke is typed the user expects to see forward progress. | |
32 The cursor should end up at the most likely place to continue editing, regardles s | |
33 of what errors may exist in previous code. It is as if the user said "I'm done | |
34 with this line. Finish it up and move me to the next line." | |
35 | |
36 ## Code Constructs | |
37 | |
38 There are a number of cases where a matching right parenthesis could be added | |
39 if it is missing. This feature has not been considered a priority since the | |
40 editor by default adds parenthesis in pairs. | |
41 | |
42 Generics are not currently handled. | |
43 | |
44 #### Declarations | |
45 | |
46 There is limited work to be done. | |
47 | |
48 - [x] Functions, methods, and classes can have a pair of braces added if they do not already have a body defined. | |
49 - [x] Functions and methods can have a closing parenthesis added to the paramete r list. | |
50 - [x] Variables can have a semicolon added to terminate them. | |
51 | |
52 #### Expressions | |
53 | |
54 Also limited. | |
55 | |
56 - [x] Unterminated strings can have appropriate string | |
57 terminators added. | |
58 - [x] Lists that have not been properly terminated can | |
59 have closing brackets added (potentially with trailing commas). | |
60 - [x] Maps are not currently handled. The parser error recovery | |
61 gets confused by braces of code blocks too easily. | |
62 | |
63 #### Statements | |
64 | |
65 With actual statements, there are many more possibilities. | |
66 Statements that start with a keyword must have at least the | |
67 keyword in the partial statement in order for completion to | |
68 happen. | |
69 | |
70 ###### Do Statement | |
71 | |
72 This is one of the few cases where an actual word may be included. | |
73 If the `while` keyword is missing it will be added. | |
74 As long as the `do` keyword is present the braces for the body | |
75 will be added. If the `while` keyword is present or can be added | |
76 then the parentheses for the condition will be added, too. Finally, | |
77 the terminating semicolon will be added. | |
78 | |
79 ###### For Statement | |
80 | |
81 The parser cannot distinguish a for-statement from a for-each unless | |
82 either at least one semicolon or the `in` keyword is present in the | |
83 control parts. If neither is present then completion cannot do any | |
84 more than possibly add braces for the body. | |
85 | |
86 Given that the statement is actually a for-statement then the control | |
87 parts will be adjusted to ensure there are two semicolons. If the braces | |
88 for the body are missing then they will be added. | |
89 | |
90 ###### For-each Statement | |
91 | |
92 Braces for the body can be added if missing. | |
93 | |
94 ###### If Statement | |
95 | |
96 The if-else-etc construct could get arbitrarily complex, so | |
97 for simplicity the `else` keyword is ignored. Starting with nothing | |
98 but the `if` keyword, the parentheses for the condition will be added | |
99 and the braces for the body will be added. | |
100 | |
101 ###### Switch Statement | |
102 | |
103 Given the `switch` keyword, parentheses for the selector will be added | |
104 if absent and the braces for the body will be added. Also, for an | |
105 individual case or default clause the terminating colon will be added | |
106 if needed. To be clear, only the colon for the clause containing | |
107 the cursor will be added. | |
108 | |
109 ###### Try Statement | |
110 | |
111 If the statement is nothing more than the `try` keyword then the braces | |
112 for the body will be added. No clauses (n, catch, or finally) will be added. | |
113 | |
114 An on-clause will be completed by adding braces for its body, if absent. | |
115 | |
116 A catch-clause will be completed by adding parentheses for its | |
117 parameter list and braces for the body. | |
118 | |
119 A finally-clause will be completed by adding braces for its body. | |
120 | |
121 ###### While Statement | |
122 | |
123 This is structurally identical to the if-statement and the implementation | |
124 for both is shared. | |
125 | |
126 ###### Expression Statements | |
127 | |
128 These include method and function invocations. | |
129 - [x] Add closing parenthesis, if the expression is an invocation. | |
130 - [x] Add terminating semicolon. | |
131 | |
132 ###### Control-flow Blocks | |
133 | |
134 After finishing a `return` or `throw` in a block that is the | |
135 body of a control-flow statement (do, for, for-each, if, while) | |
136 then the cursor will be moved outside the block, ready to begin | |
137 the next statement following the control-flow statement. | |
138 ```dart | |
139 if (isFinished()) { | |
140 releaseResources(); | |
141 return; // invoke 'smart enter' here | |
142 } | |
143 // continue typing here | |
144 ``` | |
OLD | NEW |