Skip to content

Commit 11c160e

Browse files
feat: Cumulative offset will be error
1 parent 9f43ca8 commit 11c160e

10 files changed

Lines changed: 95 additions & 40 deletions

File tree

javasm-intellij-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies {
3939
implementation("org.ow2.asm:asm-util:9.8")
4040

4141
implementation(project(":javasm-jps-plugin"))
42-
implementation("tokyo.peya:langjal:1.0.0")
42+
implementation("tokyo.peya:langjal:1.1.0")
4343
implementation("org.antlr:antlr4-intellij-adaptor:0.1")
4444
}
4545

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/editor/linenumber/InstructionOffsetCalculator.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
public class InstructionOffsetCalculator
1414
{
15-
private static final Key<InstructionOffsetCalculator> KEY = Key.create(
16-
"javasm.intellij.editor.linenumber.InstructionOffsetCalculator");
15+
private static final Key<InstructionOffsetCalculator> KEY =
16+
Key.create("javasm.intellij.editor.linenumber.InstructionOffsetCalculator");
1717

1818
private final Map<InstructionNode, Integer> instructionOffsets;
1919

@@ -35,7 +35,6 @@ private void buildOffsets(@NotNull MethodBodyNode methodBody)
3535
currentOffset += instruction.getInstructionSize();
3636
}
3737
}
38-
3938
}
4039

4140
@Nullable
@@ -50,9 +49,16 @@ public static InstructionOffsetCalculator get(@NotNull MethodBodyNode methodNode
5049
if (cached != null)
5150
return cached;
5251

53-
InstructionOffsetCalculator fresh;
54-
methodNode.putUserData(KEY, fresh = new InstructionOffsetCalculator());
55-
fresh.buildOffsets(methodNode);
52+
InstructionOffsetCalculator fresh = new InstructionOffsetCalculator();
53+
try
54+
{
55+
fresh.buildOffsets(methodNode);
56+
methodNode.putUserData(KEY, fresh);
57+
}
58+
catch (IllegalStateException ignored)
59+
{
60+
// これは,コードの記述が不完全だったり構文エラーのときにおきる。
61+
}
5662
return fresh;
5763
}
5864

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/editor/linenumber/JALCumulativeOffsetLineMarkerProvider.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ public class JALCumulativeOffsetLineMarkerProvider implements LineMarkerProvider
2525
if (instr == null)
2626
return null;
2727

28-
int cumulative = instr.getStartInstructionOffset();
28+
int cumulative;
29+
try
30+
{
31+
cumulative = instr.getStartInstructionOffset();
32+
}
33+
catch (IllegalStateException e)
34+
{
35+
// コードが不完全だったり構文エラーのときにおきる。
36+
return null;
37+
}
2938

3039
Editor editor = FileEditorManager.getInstance(element.getProject()).getSelectedTextEditor();
3140
if (editor == null)

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/formatting/JALBlock.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,29 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
2729

2830
public class JALBlock extends AbstractBlock
2931
{
3032
private final SpacingBuilder spacingBuilder;
3133

34+
private static final Map<Integer, Integer> FIXED_COLUMN_BY_THIS_RULE = Map.of(
35+
JALParser.RULE_classBody, 0,
36+
JALParser.RULE_jvmInsArgTableSwitchCaseList, 0,
37+
JALParser.RULE_jvmInsArgLookupSwitchCaseList, 0
38+
);
39+
40+
private static final Map<Integer, Integer> FIXED_COLUMN_BY_PREV_RULE = Map.of(
41+
JALParser.RULE_instruction, 2
42+
);
43+
44+
private static final Set<Integer> FIXED_COLUMN_TOKENS = Set.of(
45+
JALParser.LINE_COMMENT,
46+
JALParser.BLOCK_COMMENT,
47+
JALParser.LBR
48+
);
49+
3250
protected JALBlock(
3351
@NotNull ASTNode node,
3452
@Nullable Wrap wrap,
@@ -68,10 +86,13 @@ public Indent getIndent()
6886

6987
int ruleIndex = rule.getRuleIndex();
7088

89+
// 特定ルールにだけインデントを与える
7190
if (ruleIndex == JALParser.RULE_classMetaItem
7291
|| ruleIndex == JALParser.RULE_fieldDefinition
7392
|| ruleIndex == JALParser.RULE_methodDefinition
74-
|| ruleIndex == JALParser.RULE_instruction)
93+
|| ruleIndex == JALParser.RULE_instruction
94+
|| ruleIndex == JALParser.RULE_jvmInsArgLookupSwitchCaseList
95+
|| ruleIndex == JALParser.RULE_jvmInsArgTableSwitchCaseList)
7596
return Indent.getNormalIndent();
7697
else if (ruleIndex == JALParser.RULE_label)
7798
return Indent.getLabelIndent();
@@ -106,42 +127,41 @@ private int calcColumn(Block prevBlock)
106127
ASTNode prevNode = ((JALBlock) prevBlock).getNode();
107128
IElementType prevType = prevNode.getElementType();
108129

109-
// { のあとの調整
130+
// 現在のノード(this)のタイプ
110131
IElementType thisType = this.myNode.getElementType();
111132
if (thisType instanceof RuleIElementType rule)
112133
{
113-
int ruleIndex = rule.getRuleIndex();
114-
if (ruleIndex == JALParser.RULE_classBody)
115-
return 1; // 固定カラム
134+
Integer col = FIXED_COLUMN_BY_THIS_RULE.get(rule.getRuleIndex());
135+
if (col != null)
136+
return col;
116137
}
117138

118-
// 命令ノードなら固定カラムにする(例えば 4 カラム)
119139
if (prevType instanceof RuleIElementType rule)
120140
{
121-
int ruleIndex = rule.getRuleIndex();
122-
if (ruleIndex == JALParser.RULE_instruction)
123-
return 2; // 固定カラム
141+
Integer col = FIXED_COLUMN_BY_PREV_RULE.get(rule.getRuleIndex());
142+
if (col != null)
143+
return col;
124144
}
145+
125146
if (prevType instanceof TokenIElementType token)
126147
{
127-
int tokenIndex = token.getANTLRTokenType();
128-
if (tokenIndex == JALParser.LINE_COMMENT
129-
|| tokenIndex == JALParser.BLOCK_COMMENT
130-
|| tokenIndex == JALParser.LBR)
131-
return 2; // 固定カラム
148+
if (FIXED_COLUMN_TOKENS.contains(token.getANTLRTokenType()))
149+
return 2;
132150
}
133151

134-
// 普通の方法
135-
PsiElement psi = prevNode.getPsi();
136-
if (psi == null)
137-
return 4;
152+
return computeDocumentColumn(prevNode);
153+
}
154+
155+
private int computeDocumentColumn(ASTNode node)
156+
{
157+
PsiElement psi = node.getPsi();
158+
if (psi == null) return 4;
138159

139160
PsiFile file = psi.getContainingFile();
140161
Document doc = PsiDocumentManager.getInstance(psi.getProject()).getDocument(file);
141-
if (doc == null)
142-
return 4;
162+
if (doc == null) return 4;
143163

144-
int offset = prevNode.getTextRange().getStartOffset();
164+
int offset = node.getTextRange().getStartOffset();
145165
int lineStart = doc.getLineStartOffset(doc.getLineNumber(offset));
146166
return offset - lineStart;
147167
}

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/langjal/parser/psi/insturction/InstructionNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ public int getInstructionSize()
5555
{
5656
InstructionNameNode instructionNameNode = PsiTreeUtil.findChildOfType(this, InstructionNameNode.class);
5757
if (instructionNameNode == null)
58-
return 0;
58+
throw new IllegalStateException();
5959

6060
return instructionNameNode.getInstructionSize();
6161
}
6262

63-
public final int getStartInstructionOffset()
63+
public final int getStartInstructionOffset() throws IllegalStateException
6464
{
6565
MethodBodyNode methodBody = PsiTreeUtil.getParentOfType(this, MethodBodyNode.class);
6666
if (methodBody == null)
67-
return 0;
67+
throw new IllegalStateException();
6868

6969
InstructionOffsetCalculator calculator = InstructionOffsetCalculator.get(methodBody);
7070
Integer offset = calculator.getCumulativeOffset(this);
7171
if (offset == null)
72-
return 0;
72+
throw new IllegalStateException();
7373

7474
return offset;
7575
}

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/langjal/parser/psi/insturction/variants/xswitch/InstructionLookupSwitchNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public int getInstructionSize()
5050
{
5151
InstructionLookupSwitchArgumentNode argumentNode = this.getTableSwitchArgument();
5252
if (argumentNode == null)
53-
return 0;
53+
throw new IllegalStateException();
5454

5555
int npairs = argumentNode.getBranches().length;
5656

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/langjal/parser/psi/insturction/variants/xswitch/InstructionTableSwitchNode.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,24 @@ public int getInstructionSize()
5757
{
5858
InstructionTableSwitchArgumentNode argumentNode = this.getTableSwitchArgument();
5959
if (argumentNode == null)
60-
return 0;
60+
throw new IllegalStateException();
61+
LabelNameNode branches = argumentNode.getDefaultBranchLabelName();
62+
if (branches == null)
63+
throw new IllegalStateException();
64+
LabelNameNode[] branchLabels = argumentNode.getBranchLabels();
65+
if (branchLabels == null)
66+
throw new IllegalStateException();
6167

6268
Number lowIndexValue = argumentNode.getLowIndex();
6369
if (lowIndexValue == null)
64-
lowIndexValue = 0; // Default low index if not specified
70+
throw new IllegalStateException();
6571
int lowIndex = lowIndexValue.intValue();
66-
int highIndex = lowIndex + argumentNode.getBranchLabels().length - 1;
72+
return calcActualSize(lowIndex, branchLabels);
73+
}
74+
75+
private int calcActualSize(int lowIndex, LabelNameNode[] branchLabels)
76+
{
77+
int highIndex = lowIndex + branchLabels.length - 1;
6778
int nPairs = highIndex - lowIndex + 1;
6879

6980
int baseOffset = this.getStartInstructionOffset(); // assume you have this

javasm-intellij-plugin/src/main/java/tokyo/peya/javasm/intellij/stackviewer/StackFrameInfoController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,16 @@ private void showCurrentLineStackFrame(@NotNull Editor editor)
185185

186186
String methodName = methodNode.getMethodName();
187187
String methodDesc = methodNode.getMethodDescriptor().getDescriptorString();
188-
int offset = node.getStartInstructionOffset();
188+
int offset;
189+
try
190+
{
191+
offset = node.getStartInstructionOffset();
192+
}
193+
catch (IllegalStateException e)
194+
{
195+
// コードが不完全だったり構文エラーのときにおきる。
196+
return;
197+
}
189198

190199
// スタックフレームの情報を取得する。
191200
InstructionUIElement instruction = result.getInstructionAt(methodName, methodDesc, offset);

javasm-intellij-plugin/src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<compiler.buildTargetScopeProvider
3939
implementation="tokyo.peya.javasm.intellij.execution.JALBuildTargetScopeProvider"/>
4040
<compileServer.plugin
41-
classpath="javasm-jps-plugin-1.0.0.jar;langjal-1.0.0.jar;antlr4-runtime-4.13.2.jar;icu4j-72.1.jar;org.abego.treelayout.core-1.0.3.jar;ST4-4.3.4.jar;asm-9.8.jar;asm-commons-9.8.jar;asm-util-9.8.jar;asm-tree-9.8.jar;asm-analysis-9.8.jar;"/>
41+
classpath="javasm-jps-plugin-1.0.0.jar;langjal-1.1.0.jar;antlr4-runtime-4.13.2.jar;icu4j-72.1.jar;org.abego.treelayout.core-1.0.3.jar;ST4-4.3.4.jar;asm-9.8.jar;asm-commons-9.8.jar;asm-util-9.8.jar;asm-tree-9.8.jar;asm-analysis-9.8.jar;"/>
4242
<lang.ast.factory language="JAL" implementationClass="tokyo.peya.javasm.intellij.langjal.parser.JALASTFactory"/>
4343
<lang.psiStructureViewFactory language="JAL"
4444
implementationClass="tokyo.peya.javasm.intellij.editor.structureview.JALStructureViewFactory"/>

javasm-jps-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies {
3030
implementation("org.ow2.asm:asm-util:9.8")
3131

3232
compileOnly("com.google.guava:guava:33.4.8-jre")
33-
implementation("tokyo.peya:langjal:1.0.0")
33+
implementation("tokyo.peya:langjal:1.1.0")
3434
}
3535

3636
tasks {

0 commit comments

Comments
 (0)