-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add additional intentions for converting literals to binary and decim…
…al conversions
- Loading branch information
Showing
14 changed files
with
228 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/org/ca65/action/ConvertNumberToBinaryIntentionAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.ca65.action; | ||
|
||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction; | ||
import com.intellij.codeInspection.util.IntentionFamilyName; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.ca65.Asm6502Bundle; | ||
import org.ca65.psi.AsmElementFactory; | ||
import org.ca65.psi.AsmFile; | ||
import org.ca65.psi.AsmNumericLiteral; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static org.ca65.action.IntentionActionUtil.*; | ||
|
||
public class ConvertNumberToBinaryIntentionAction extends BaseIntentionAction { | ||
@Override | ||
public @NotNull @IntentionFamilyName String getFamilyName() { | ||
return Asm6502Bundle.message("INTN.NAME.convert.to.bin"); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (!(file instanceof AsmFile)) { | ||
return false; | ||
} | ||
AsmNumericLiteral literal = getAsmNumericLiteral(editor, file); | ||
if (literal == null) { // Caret is not over a numeric literal | ||
return false; | ||
} | ||
String text = literal.getText(); | ||
if (!canConvertToBinary(text)) { | ||
return false; | ||
} | ||
setText(Asm6502Bundle.message("INTN.convert.to.bin", literal.getText())); | ||
return true; | ||
} | ||
|
||
private static boolean canConvertToBinary(String text) { | ||
return isConvertibleDec(text) || isConvertibleHex(text); | ||
} | ||
|
||
private static String doConvertToBinary(String str) { | ||
// Parse | ||
final int intValue; | ||
if(str.startsWith("$")) { | ||
intValue = Integer.parseInt(str.substring(1), 16); // From hex eg. "$ff" | ||
} else { | ||
intValue = Integer.parseInt(str, 10); // From dec eg. "42" | ||
} | ||
String binString = Integer.toBinaryString(intValue); | ||
// Pad to 8 bit, 16 bit, 24-bit values. | ||
int currentLen = binString.length(); | ||
int remainder = currentLen % 8; | ||
if(remainder != 0) { | ||
// Pad to multiple of 8 bits | ||
binString = StringUtils.leftPad(binString, (currentLen + 8) - remainder, "0"); | ||
} | ||
return "%" + binString; // Prefixed with % | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
AsmNumericLiteral literal = getAsmNumericLiteral(editor, file); | ||
if(literal == null || !canConvertToBinary(literal.getText())) { | ||
// Some weirdness if this happens | ||
return; | ||
} | ||
String replacement = doConvertToBinary(literal.getText()); | ||
PsiElement newLiteral = AsmElementFactory.createNumericLiteral(project, replacement); | ||
literal.replace(newLiteral); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/ca65/action/ConvertNumberToDecimalIntentionAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.ca65.action; | ||
|
||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction; | ||
import com.intellij.codeInspection.util.IntentionFamilyName; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.ca65.Asm6502Bundle; | ||
import org.ca65.psi.AsmElementFactory; | ||
import org.ca65.psi.AsmFile; | ||
import org.ca65.psi.AsmNumericLiteral; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static org.ca65.action.IntentionActionUtil.*; | ||
|
||
public class ConvertNumberToDecimalIntentionAction extends BaseIntentionAction { | ||
@Override | ||
public @NotNull @IntentionFamilyName String getFamilyName() { | ||
return Asm6502Bundle.message("INTN.NAME.convert.to.dec"); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (!(file instanceof AsmFile)) { | ||
return false; | ||
} | ||
AsmNumericLiteral literal = getAsmNumericLiteral(editor, file); | ||
if (literal == null) { // Caret is not over a numeric literal | ||
return false; | ||
} | ||
String text = literal.getText(); | ||
if (!canConvertToDecimal(text)) { | ||
return false; | ||
} | ||
setText(Asm6502Bundle.message("INTN.convert.to.dec", literal.getText())); | ||
return true; | ||
} | ||
|
||
private static boolean canConvertToDecimal(String text) { | ||
return isConvertibleBin(text) || isConvertibleHex(text); | ||
} | ||
|
||
private static String doConvertToDecimal(String str) { | ||
// Parse | ||
final int intValue; | ||
if(str.startsWith("%")) { | ||
intValue = Integer.parseInt(str.substring(1), 2); // From bin eg. "%01010" | ||
} else { | ||
intValue = Integer.parseInt(str.substring(1), 16); // From hex eg. "$ff" | ||
} | ||
return Integer.toString(intValue); | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
AsmNumericLiteral literal = getAsmNumericLiteral(editor, file); | ||
if(literal == null || !canConvertToDecimal(literal.getText())) { | ||
// Some weirdness if this happens | ||
return; | ||
} | ||
String replacement = doConvertToDecimal(literal.getText()); | ||
PsiElement newLiteral = AsmElementFactory.createNumericLiteral(project, replacement); | ||
literal.replace(newLiteral); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.ca65.action; | ||
|
||
import com.intellij.codeInsight.TargetElementUtilBase; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import org.ca65.psi.AsmNumericLiteral; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class IntentionActionUtil { | ||
@Nullable | ||
public static AsmNumericLiteral getAsmNumericLiteral(Editor editor, PsiFile file) { | ||
final int offset = TargetElementUtilBase.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset()); | ||
return PsiTreeUtil.getParentOfType(file.findElementAt(offset), AsmNumericLiteral.class); | ||
} | ||
|
||
public static boolean isConvertibleHex(String text) { | ||
if(!text.startsWith("$")) { // Already in hex | ||
return false; | ||
} | ||
return text.length() > 1 && text.length() <= 7; | ||
} | ||
|
||
public static boolean isConvertibleBin(String text) { | ||
if(!text.startsWith("%")) { | ||
return false; | ||
} | ||
// In binary, just do length check for 24 bits max. | ||
return text.length() > 1 && text.length() <= 25; | ||
} | ||
|
||
public static boolean isConvertibleDec(String text) { | ||
if(text.startsWith("%") || text.startsWith("$")) { | ||
return false; | ||
} | ||
// Decimal literal. Check length first to avoid overflow weirdness | ||
int maxValue = 16777215; | ||
if(text.length() > Integer.toString(maxValue).length()) { | ||
return false; | ||
} | ||
int parsedValue = Integer.parseInt(text); | ||
return parsedValue >= 0 && parsedValue <= maxValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ain/resources/intentionDescriptions/ConvertNumberToBinaryIntentionAction/after.s.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lda #%00101010 |
1 change: 1 addition & 0 deletions
1
...in/resources/intentionDescriptions/ConvertNumberToBinaryIntentionAction/before.s.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lda #42 |
7 changes: 7 additions & 0 deletions
7
...ain/resources/intentionDescriptions/ConvertNumberToBinaryIntentionAction/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
Replaces a numeric literal (decimal or hexadecimal) with an equivalent binary value. | ||
|
||
This intention is available for decimal or hexadecimal literals of up to 24 bits. The result will be represented as an 8, 16, or 24 digit binary number, padded with '0' if necessary. | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
...in/resources/intentionDescriptions/ConvertNumberToDecimalIntentionAction/after.s.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sta $5fff |
1 change: 1 addition & 0 deletions
1
...n/resources/intentionDescriptions/ConvertNumberToDecimalIntentionAction/before.s.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sta 24575 |
7 changes: 7 additions & 0 deletions
7
...in/resources/intentionDescriptions/ConvertNumberToDecimalIntentionAction/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
Replaces a numeric literal (hexadecimal or binary) with an equivalent decimal value. | ||
|
||
This intention is available for hexadecimal or binary literals of up to 24 bits. | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters