Configurable code formatting (Indentation + brace position)

This commit is contained in:
Jindra Pet��k
2014-02-25 15:52:41 +01:00
parent 856765011c
commit 1e3a67ec1c
36 changed files with 623 additions and 578 deletions
@@ -664,24 +664,6 @@ public class ABC {
return null;
}
public static String addTabs(String s, int tabs) {
String[] parts = s.split("\r\n");
if (!s.contains("\r\n")) {
parts = s.split("\n");
}
StringBuilder ret = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
for (int t = 0; t < tabs; t++) {
ret.append(HilightedTextWriter.INDENT_STRING);
}
ret.append(parts[i]);
if (i < parts.length - 1) {
ret.append("\r\n");
}
}
return ret.toString();
}
public boolean isStaticTraitId(int classIndex, int traitId) {
if (traitId < class_info[classIndex].static_traits.traits.length) {
return true;
@@ -188,7 +188,7 @@ public class ScriptPack implements TreeElementItem {
String fileName = outDir.toString() + File.separator + Helper.makeFileName(scriptName) + ".as";
File file = new File(fileName);
try (FileTextWriter writer = new FileTextWriter(new FileOutputStream(file))) {
try (FileTextWriter writer = new FileTextWriter(Configuration.getCodeFormatting(),new FileOutputStream(file))) {
try {
toSource(writer, abcList, abc.script_info[scriptIndex].traits.traits, exportMode, parallel);
} catch (InterruptedException ex) {
@@ -2247,7 +2247,7 @@ public class AVM2Code implements Serializable {
invalidateCache();
try {
List<Integer> outputMap = new ArrayList<>();
HilightedTextWriter writer = new HilightedTextWriter(false);
HilightedTextWriter writer = new HilightedTextWriter(Configuration.getCodeFormatting(),false);
toASMSource(constants, trait, info, body, outputMap, ExportMode.PCODE, writer);
String src = writer.toString();
@@ -2289,7 +2289,7 @@ public class AVM2Code implements Serializable {
public void removeIgnored(ConstantPool constants, Trait trait, MethodInfo info, MethodBody body) throws InterruptedException {
try {
List<Integer> outputMap = new ArrayList<>();
HilightedTextWriter writer = new HilightedTextWriter(false);
HilightedTextWriter writer = new HilightedTextWriter(Configuration.getCodeFormatting(),false);
toASMSource(constants, trait, info, body, outputMap, ExportMode.PCODE, writer);
String src = writer.toString();
AVM2Code acode = ASM3Parser.parse(new StringReader(src), constants, trait, body, info);
@@ -24,6 +24,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition;
import com.jpexs.decompiler.flash.abc.avm2.model.UnparsedAVM2Item;
import com.jpexs.decompiler.flash.abc.types.MethodBody;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.model.LocalData;
@@ -40,7 +41,7 @@ public class NewClassIns extends InstructionDefinition {
@Override
public void translate(boolean isStatic, int scriptIndex, int classIndex, java.util.HashMap<Integer, GraphTargetItem> localRegs, Stack<GraphTargetItem> stack, java.util.Stack<GraphTargetItem> scopeStack, ConstantPool constants, AVM2Instruction ins, MethodInfo[] method_info, List<GraphTargetItem> output, MethodBody body, ABC abc, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames, String path, HashMap<Integer, Integer> localRegsAssignmentIps, int ip, HashMap<Integer, List<Integer>> refs, AVM2Code code) throws InterruptedException {
int clsIndex = ins.operands[0];
HilightedTextWriter writer = new HilightedTextWriter(false);
HilightedTextWriter writer = new HilightedTextWriter(Configuration.getCodeFormatting(),false);
stack.pop().toString(writer, LocalData.create(constants, localRegNames, fullyQualifiedNames));
String baseType = writer.toString();
stack.push(new UnparsedAVM2Item(ins, "new " + abc.constants.getMultiname(abc.instance_info[clsIndex].name_index).getName(constants, fullyQualifiedNames) + ".class extends " + baseType));
@@ -27,6 +27,7 @@ import com.jpexs.decompiler.flash.abc.avm2.model.FullMultinameAVM2Item;
import com.jpexs.decompiler.flash.abc.avm2.model.SetSuperAVM2Item;
import com.jpexs.decompiler.flash.abc.types.MethodBody;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.model.LocalData;
@@ -54,7 +55,7 @@ public class SetSuperIns extends InstructionDefinition implements SetTypeIns {
public String getObject(Stack<AVM2Item> stack, ABC abc, AVM2Instruction ins, List<AVM2Item> output, MethodBody body, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) throws InterruptedException {
int multinameIndex = ins.operands[0];
String multiname = resolveMultinameNoPop(1, stack, abc.constants, multinameIndex, ins, fullyQualifiedNames);
HilightedTextWriter writer = new HilightedTextWriter(false);
HilightedTextWriter writer = new HilightedTextWriter(Configuration.getCodeFormatting(),false);
stack.get(1 + resolvedCount(abc.constants, multinameIndex)).toString(writer, LocalData.create(abc.constants, localRegNames, fullyQualifiedNames));
String obj = writer.toString();
return obj + ".super." + multiname;
@@ -20,6 +20,7 @@ import com.jpexs.decompiler.flash.abc.ABC;
import com.jpexs.decompiler.flash.abc.types.traits.TraitMethodGetterSetter;
import com.jpexs.decompiler.flash.abc.types.traits.TraitSlotConst;
import com.jpexs.decompiler.flash.abc.types.traits.Traits;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.flash.helpers.NulWriter;
import com.jpexs.decompiler.flash.tags.ABCContainerTag;
@@ -49,7 +50,7 @@ public abstract class ConstVarMultinameUsage extends TraitMultinameUsage {
}
((TraitSlotConst) traits.traits[traitIndex]).convertHeader(null, "", abcTags, abc, isStatic, ExportMode.SOURCE, -1/*FIXME*/, classIndex, nulWriter, new ArrayList<String>(), false);
HilightedTextWriter writer = new HilightedTextWriter(false);
HilightedTextWriter writer = new HilightedTextWriter(Configuration.getCodeFormatting(),false);
writer.appendNoHilight(super.toString(abcTags, abc) + " ");
if (parentTraitIndex > -1) {
if (isStatic) {
@@ -19,6 +19,7 @@ package com.jpexs.decompiler.flash.abc.usages;
import com.jpexs.decompiler.flash.abc.ABC;
import com.jpexs.decompiler.flash.abc.types.traits.TraitMethodGetterSetter;
import com.jpexs.decompiler.flash.abc.types.traits.Traits;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.flash.helpers.NulWriter;
import com.jpexs.decompiler.flash.tags.ABCContainerTag;
@@ -57,7 +58,7 @@ public abstract class MethodMultinameUsage extends TraitMultinameUsage {
((TraitMethodGetterSetter) traits.traits[traitIndex]).convertHeader(null, "", abcTags, abc, isStatic, ExportMode.SOURCE, -1/*FIXME*/, classIndex, nulWriter, new ArrayList<String>(), false);
}
HilightedTextWriter writer = new HilightedTextWriter(false);
HilightedTextWriter writer = new HilightedTextWriter(Configuration.getCodeFormatting(),false);
writer.appendNoHilight(super.toString(abcTags, abc));
writer.appendNoHilight(" ");
if (isInitializer) {