Skip to content

Commit d524095

Browse files
committed
feat(devins-lang): add support for line info in commands
This commit introduces the concept of line info in DevIns commands, enhancing the language's capabilities. The line info feature allows for more precise control over AI agent actions by specifying the exact line and column numbers within a code block. This enhancement is achieved by adding a new token type 'LINE_INFO' to the DevIns lexer and parser, and corresponding highlighting in the syntax highlighter.
1 parent 5c2c7fd commit d524095

File tree

6 files changed

+70
-5
lines changed

6 files changed

+70
-5
lines changed

exts/devins-lang/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ interaction with LLM, we later renamed it to DevIns. This change was prompted by
2424
AI company, to avoid confusion. The name DevIns, derived from DevInstruction, succinctly captures the essence of the
2525
language in guiding the AI Agent's actions.
2626

27+
## AutoDev/Netscape Navigator
28+
29+
Return Code:
30+
31+
```devin
32+
/symbol:cc.unitmesh.devti.language.lexer.DevInsLexer.parse
33+
```
34+
35+
Hyperlink to the IDE:
36+
37+
```devin
38+
/link:cc.unitmesh.devti.language.lexer.DevInsLexer.parse
39+
```
40+
41+
L1C2 = Line 1 Column 2
42+
43+
```devin
44+
/file:cpp/src/main/kotlin/cc/unitmesh/cpp/context/CppFileContextBuilder.kt#L1C2-L23
45+
```

exts/devins-lang/src/grammar/DevInLexer.flex

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.intellij.lexer.FlexLexer;
44
import com.intellij.psi.tree.IElementType;
55
import static cc.unitmesh.devti.language.psi.DevInTypes.*;
66
import com.intellij.psi.TokenType;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
79

810
%%
911

@@ -33,6 +35,7 @@ import com.intellij.psi.TokenType;
3335

3436
%s CODE_BLOCK
3537
%s COMMENT_BLOCK
38+
%s LINE_BLOCK
3639

3740
%s LANG_ID
3841

@@ -44,8 +47,11 @@ COMMAND_ID=[a-zA-Z0-9][_\-a-zA-Z0-9]*
4447
LANGUAGE_ID=[a-zA-Z][_\-a-zA-Z0-9 .]*
4548
SYSTEM_ID=[a-zA-Z][_\-a-zA-Z0-9]*
4649
NUMBER=[0-9]+
47-
4850
TEXT_SEGMENT=[^$/@#\n]+
51+
52+
// READ LINE FORMAT: L2C2-L0C100 or L1-L1
53+
LINE_INFO=L[0-9]+(C[0-9]+)?(-L[0-9]+(C[0-9]+)?)?
54+
//LINE_INFO=[L][0-9]+[L][0-9]+
4955
COMMAND_PROP=[^\ \t\r\n]*
5056
CODE_CONTENT=[^\n]+
5157
COMMENTS=\[ ([^\]]+)? \] [^\t\r\n]*
@@ -118,6 +124,33 @@ SHARP=#
118124
return TEXT_SEGMENT;
119125
}
120126
}
127+
128+
private IElementType command_value() {
129+
String text = yytext().toString().trim();
130+
String [] split = text.split("#");
131+
132+
if (split.length == 1) {
133+
return COMMAND_PROP;
134+
}
135+
136+
// split by # if it is a line info
137+
String last = split[split.length - 1];
138+
Pattern compile = Pattern.compile("L\\d+(C\\d+)?(-L\\d+(C\\d+)?)?");
139+
Matcher matcher = compile.matcher(last);
140+
if (matcher.matches()) {
141+
// before # is command prop, after # is line info
142+
int number = last.length() + "#".length();
143+
if (number > 0) {
144+
yypushback(number);
145+
yybegin(LINE_BLOCK);
146+
return COMMAND_PROP;
147+
} else {
148+
return COMMAND_PROP;
149+
}
150+
}
151+
152+
return COMMAND_PROP;
153+
}
121154
%}
122155

123156
%%
@@ -154,11 +187,17 @@ SHARP=#
154187
}
155188

156189
<COMMAND_VALUE_BLOCK> {
157-
{COMMAND_PROP} { return COMMAND_PROP; }
190+
{COMMAND_PROP} { return command_value(); }
158191
" " { yypushback(1); yybegin(YYINITIAL); }
159192
[^] { yypushback(1); yybegin(YYINITIAL); }
160193
}
161194

195+
<LINE_BLOCK> {
196+
{LINE_INFO} { return LINE_INFO; }
197+
{SHARP} { return SHARP; }
198+
[^] { yypushback(yylength()); yybegin(COMMAND_VALUE_BLOCK); }
199+
}
200+
162201
<AGENT_BLOCK> {
163202
{AGENT_ID} { yybegin(YYINITIAL); return AGENT_ID; }
164203
[^] { return TokenType.BAD_CHARACTER; }

exts/devins-lang/src/grammar/DevInParser.bnf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
COLON = "COLON"
3333
COMMAND_PROP = "COMMAND_PROP"
3434
SHARP = "SHARP"
35+
LINE_INFO = "LINE_INFO"
3536
]
3637
}
3738

3839
DevInsFile ::= (used | code | TEXT_SEGMENT | NEWLINE | COMMENTS)*
3940

4041
used ::= (
4142
AGENT_START AGENT_ID
42-
| COMMAND_START COMMAND_ID (COLON COMMAND_PROP?)?
43+
| COMMAND_START COMMAND_ID (COLON COMMAND_PROP (SHARP LINE_INFO)?)?
4344
| VARIABLE_START VARIABLE_ID
4445
| SYSTEM_START SYSTEM_ID COLON NUMBER
4546
)

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/highlight/DevInSyntaxHighlighter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class DevInSyntaxHighlighter : SyntaxHighlighter {
3232
ATTRIBUTES[DevInTypes.COMMAND_ID] = DefaultLanguageHighlighterColors.KEYWORD
3333
ATTRIBUTES[DevInTypes.COMMAND_PROP] = DefaultLanguageHighlighterColors.STRING
3434

35+
ATTRIBUTES[DevInTypes.LINE_INFO] = DefaultLanguageHighlighterColors.NUMBER
36+
3537
ATTRIBUTES[DevInTypes.CODE_BLOCK_START] = DefaultLanguageHighlighterColors.KEYWORD
3638
ATTRIBUTES[DevInTypes.CODE_BLOCK_END] = DefaultLanguageHighlighterColors.KEYWORD
3739
ATTRIBUTES[DevInTypes.LANGUAGE_ID] = DefaultLanguageHighlighterColors.CONSTANT

exts/devins-lang/src/test/testData/parser/AutoCommand.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ DevInFile
33
PsiElement(DevInTokenType.COMMAND_START)('/')
44
PsiElement(DevInTokenType.COMMAND_ID)('write')
55
PsiElement(DevInTokenType.COLON)(':')
6-
PsiElement(DevInTokenType.COMMAND_PROP)('Sample.file#L1-L12')
6+
PsiElement(DevInTokenType.COMMAND_PROP)('Sample.file')
7+
PsiElement(DevInTokenType.SHARP)('#')
8+
PsiElement(DevInTokenType.LINE_INFO)('L1-L12')

exts/devins-lang/src/test/testData/parser/CommandAndSymbol.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ DevInFile
2525
PsiElement(DevInTokenType.COMMAND_START)('/')
2626
PsiElement(DevInTokenType.COMMAND_ID)('write')
2727
PsiElement(DevInTokenType.COLON)(':')
28-
PsiElement(DevInTokenType.COMMAND_PROP)('presentation/VirtualFilePresentation.java#L1-L12')
28+
PsiElement(DevInTokenType.COMMAND_PROP)('presentation/VirtualFilePresentation.java')
29+
PsiElement(DevInTokenType.SHARP)('#')
30+
PsiElement(DevInTokenType.LINE_INFO)('L1-L12')

0 commit comments

Comments
 (0)