Skip to content

Commit

Permalink
code update
Browse files Browse the repository at this point in the history
  • Loading branch information
darkengine committed Sep 7, 2024
1 parent 8e58507 commit 4ae8b0c
Show file tree
Hide file tree
Showing 25 changed files with 1,792 additions and 58 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation libs.javaparser.core

implementation project(":nodes")
implementation project(":codeview")

implementation (libs.android.code.view){
exclude group: 'com.android.support', module: 'support-compat'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/dark/androidbox/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;

import java.io.StringReader;
import java.util.List;
Expand Down Expand Up @@ -38,4 +39,8 @@ public List<MethodDeclaration> getConstructors() {
.filter(method -> method.getParameters().isEmpty())
.collect(Collectors.toList());
}

public List<MethodCallExpr> getMethodCalls() {
return unit.findAll(MethodCallExpr.class);
}
}
172 changes: 116 additions & 56 deletions app/src/main/java/com/dark/androidbox/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,73 @@
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.dark.androidbox.adapter.NodeViewAdapter;
import com.dark.androidbox.codeView.Editor;
import com.dark.androidbox.databinding.ActivityMainBinding;
import com.dark.androidbox.model.NodeData;
import com.dark.androidbox.types.NodeTypes;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.gyso.treeview.GysoTreeView;
import com.gyso.treeview.adapter.TreeViewAdapter;
import com.gyso.treeview.layout.BoxDownTreeLayoutManager;
import com.gyso.treeview.line.SmoothLine;
import com.gyso.treeview.model.NodeModel;
import com.gyso.treeview.model.TreeModel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;
private GysoTreeView treeView;
private TreeViewAdapter<NodeData> adapter;
private Editor editor;
private Lexer lexer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

editor = new Editor(binding.code);
lexer = new Lexer(codeStr());

binding.code.setText(codeStr());

treeView = binding.nodeView.treeview;
setupSyntaxHighlighter();


for (FieldDeclaration declaration : lexer.getFields()) {
editor.setTxtColor(declaration.getVariables().get(0).getTypeAsString(), Color.parseColor("#e4c17b"));
}

for (FieldDeclaration declaration : lexer.getFields()) {
editor.setTxtColor(declaration.getVariables().get(0).getNameAsString(), Color.parseColor("#ee596e"));
}

binding.btn.setOnClickListener(view -> {
String codeText = binding.code.getText().toString();
if (!codeText.isEmpty()) {
loadJava(new StringBuilder(codeText));
loadJava();
}
});
}

private void loadJava(StringBuilder code) {
Lexer lexer = new Lexer(code);
private void loadJava() {

adapter = new NodeViewAdapter();
initializeTreeView();

NodeModel<NodeData> node0 = createNode(lexer.getClasses().get(0), NodeTypes.CLASSES);
NodeModel<NodeData> node0 = createNode(new NodeData(lexer.getClasses().get(0).getNameAsString(), lexer.getClasses().get(0).toString(), NodeTypes.CLASSES));
TreeModel<NodeData> treeModel = new TreeModel<>(node0);

treeModel.addNode(
createNode(lexer.getMethods().get(0), NodeTypes.METHODS),
createNode(lexer.getFields().get(0).getVariables().get(0), NodeTypes.VARIABLES)
);
treeModel.addNode(node0,
createNode(new NodeData(lexer.getMethods().get(0).getNameAsString(), lexer.getMethods().get(0).toString(), NodeTypes.METHODS)),
createNode(new NodeData(lexer.getFields().get(0).getVariables().get(0).getNameAsString(), lexer.getFields().get(0).getVariables().get(0).getTypeAsString(), NodeTypes.VARIABLES)));


adapter.setTreeModel(treeModel);
updateUI();
Expand All @@ -70,8 +80,8 @@ private void initializeTreeView() {
treeView.setTreeLayoutManager(new BoxDownTreeLayoutManager(this, 20, 20, new SmoothLine()));
}

private NodeModel<NodeData> createNode(Object item, NodeTypes type) {
return new NodeModel<>(new NodeData(item.toString(), item.toString(), type));
private NodeModel<NodeData> createNode(NodeData data) {
return new NodeModel<>(data);
}

private void updateUI() {
Expand All @@ -80,45 +90,95 @@ private void updateUI() {
new Handler(getMainLooper()).postDelayed(() -> treeView.getEditor().focusMidLocation(), 2000);
}

private void setupSyntaxHighlighter() {
binding.code.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
removeExistingSpans(s);
highlightSyntax(s);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
}

private void removeExistingSpans(Editable s) {
ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
for (ForegroundColorSpan span : spans) {
s.removeSpan(span);
}
}
private StringBuilder codeStr() {
return new StringBuilder("package com.dark.androidbox;\n" +
"\n" +
"import android.graphics.Color;\n" +
"import android.os.Bundle;\n" +
"import android.os.Handler;\n" +
"import android.text.Editable;\n" +
"import android.text.Spannable;\n" +
"import android.text.TextWatcher;\n" +
"import android.text.style.ForegroundColorSpan;\n" +
"import android.view.View;\n" +
"\n" +
"import androidx.appcompat.app.AppCompatActivity;\n" +
"\n" +
"import com.dark.androidbox.adapter.NodeViewAdapter;\n" +
"import com.dark.androidbox.codeView.Editor;\n" +
"import com.dark.androidbox.databinding.ActivityMainBinding;\n" +
"import com.dark.androidbox.model.NodeData;\n" +
"import com.dark.androidbox.types.NodeTypes;\n" +
"import com.gyso.treeview.GysoTreeView;\n" +
"import com.gyso.treeview.adapter.TreeViewAdapter;\n" +
"import com.gyso.treeview.layout.BoxDownTreeLayoutManager;\n" +
"import com.gyso.treeview.line.SmoothLine;\n" +
"import com.gyso.treeview.model.NodeModel;\n" +
"import com.gyso.treeview.model.TreeModel;\n" +
"\n" +
"import java.util.regex.Matcher;\n" +
"import java.util.regex.Pattern;\n" +
"\n" +
"public class MainActivity extends AppCompatActivity {\n" +
"\n" +
" private ActivityMainBinding binding;\n" +
" private GysoTreeView treeView;\n" +
" private TreeViewAdapter<NodeData> adapter;\n" +
"\n" +
" @Override\n" +
" protected void onCreate(Bundle savedInstanceState) {\n" +
" super.onCreate(savedInstanceState);\n" +
" binding = ActivityMainBinding.inflate(getLayoutInflater());\n" +
" setContentView(binding.getRoot());\n" +
"\n" +
" Editor editor = new Editor(binding.code);\n" +
" \n" +
" binding.code.setText();\n" +
"\n" +
" treeView = binding.nodeView.treeview;\n" +
"\n" +
" binding.btn.setOnClickListener(view -> {\n" +
" String codeText = binding.code.getText().toString();\n" +
" if (!codeText.isEmpty()) {\n" +
" loadJava(new StringBuilder(codeText));\n" +
" }\n" +
" });\n" +
" }\n" +
"\n" +
" private void loadJava(StringBuilder code) {\n" +
" Lexer lexer = new Lexer(code);\n" +
" adapter = new NodeViewAdapter();\n" +
" initializeTreeView();\n" +
"\n" +
" NodeModel<NodeData> node0 = createNode(lexer.getClasses().get(0), NodeTypes.CLASSES);\n" +
" TreeModel<NodeData> treeModel = new TreeModel<>(node0);\n" +
"\n" +
" treeModel.addNode(\n" +
" createNode(lexer.getMethods().get(0), NodeTypes.METHODS),\n" +
" createNode(lexer.getFields().get(0).getVariables().get(0), NodeTypes.VARIABLES)\n" +
" );\n" +
"\n" +
" adapter.setTreeModel(treeModel);\n" +
" updateUI();\n" +
" }\n" +
"\n" +
" private void initializeTreeView() {\n" +
" treeView.setAdapter(adapter);\n" +
" treeView.setTreeLayoutManager(new BoxDownTreeLayoutManager(this, 20, 20, new SmoothLine()));\n" +
" }\n" +
"\n" +
" private NodeModel<NodeData> createNode(Object item, NodeTypes type) {\n" +
" return new NodeModel<>(new NodeData(item.toString(), item.toString(), type));\n" +
" }\n" +
"\n" +
" private void updateUI() {\n" +
" binding.btn.setVisibility(View.GONE);\n" +
" binding.code.setVisibility(View.GONE);\n" +
" new Handler(getMainLooper()).postDelayed(() -> treeView.getEditor().focusMidLocation(), 2000);\n" +
" }\n" +
"\n" +
"}\n");

private void highlightSyntax(Editable s) {
applyPattern(s, "\\b(abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|native|new|null|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|transient|try|void|volatile|while)\\b", Color.parseColor("#186fa1"));
applyPattern(s, "//.*|/\\*(?:.|[\\n\\r])*?\\*/", Color.GRAY);
applyPattern(s, "\"(.*?)\"", Color.parseColor("#32872f"));
applyPattern(s, "\\b\\d+\\b", Color.parseColor("#d35400"));
applyPattern(s, "@\\w+", Color.parseColor("#9b59b6"));
applyPattern(s, "\\b(int|char|float|double|boolean|long|short|byte)\\b", Color.parseColor("#2c3e50"));
}

private void applyPattern(Editable s, String pattern, int color) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(s);
while (m.find()) {
s.setSpan(new ForegroundColorSpan(color), m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
73 changes: 73 additions & 0 deletions app/src/main/java/com/dark/androidbox/codeView/Editor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.dark.androidbox.codeView;

import android.graphics.Color;
import com.amrdeveloper.codeview.CodeView;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class Editor {
public CodeView txtCode;

// Patterns for syntax highlighting
private final Pattern keywordsPattern = Pattern.compile(
"\\b(void|public|private|protected|static|final|new|return|int|short|long|float|double|boolean|if|else|for|while|do|switch|case|break|continue|try|catch|finally|throw|throws|interface|extends|implements|package|import)\\b");
private final Pattern classPattern = Pattern.compile(
"\\b(String|Array|ArrayList|HashMap|Map|Pattern)\\b");
private final Pattern methodPattern = Pattern.compile(
"\\b([a-zA-Z_$][a-zA-Z\\d_$]*)(\\s*\\()");
private final Pattern variablePattern = Pattern.compile(
"(public|private|protected)?\\s*(static)?\\s*(final)?\\s*([A-Za-z0-9_$<>]+)\\s+([a-zA-Z_$][a-zA-Z\\d_$]*)\\s*([=;])");
private final Pattern packagePattern = Pattern.compile(
"(?<=import\\s)([\\w.]+\\*?)(?=;)");
private final Pattern commentPattern = Pattern.compile(
"/\\*(?:[^*]|\\*(?!/))*\\*/|//.*");

// Colors for syntax highlighting
private static final int KEYWORD_COLOR = Color.parseColor("#B57AFF"); // Purple
private static final int CLASS_COLOR = Color.parseColor("#A9B7C6"); // Light gray
private static final int METHOD_COLOR = Color.parseColor("#469ded"); // Yellow
private static final int VARIABLE_COLOR = Color.parseColor("#9876AA"); // Purple
private static final int PACKAGE_COLOR = Color.parseColor("#6897BB"); // Blue
private static final int COMMENT_COLOR = Color.parseColor("#808080"); // Gray

public Editor(CodeView codeView) {
this.txtCode = codeView;
setUp();
}

private void setUp() {
// Setup line numbering
txtCode.setEnableLineNumber(true);
txtCode.setLineNumberTextColor(Color.parseColor("#606366"));
txtCode.setLineNumberTextSize(30);

// Setup auto-complete for code pairs
Map<Character, Character> pairCompleteMap = new HashMap<>();
pairCompleteMap.put('{', '}');
pairCompleteMap.put('[', ']');
pairCompleteMap.put('(', ')');
pairCompleteMap.put('<', '>');
pairCompleteMap.put('"', '"');
txtCode.enablePairComplete(true);
txtCode.setPairCompleteMap(pairCompleteMap);

// Add syntax highlighting patterns
txtCode.addSyntaxPattern(keywordsPattern, KEYWORD_COLOR);
// txtCode.addSyntaxPattern(classPattern, CLASS_COLOR);
txtCode.addSyntaxPattern(methodPattern, METHOD_COLOR);
//txtCode.addSyntaxPattern(variablePattern, VARIABLE_COLOR);
txtCode.addSyntaxPattern(packagePattern, PACKAGE_COLOR);
txtCode.addSyntaxPattern(commentPattern, COMMENT_COLOR);

// Set editor background color
txtCode.setBackgroundColor(Color.parseColor("#2B2B2B"));
txtCode.setTextColor(Color.parseColor("#A9B7C6"));
}

public void setTxtColor(String targetString, int color) {
Pattern pattern = Pattern.compile(Pattern.quote(targetString));
txtCode.addSyntaxPattern(pattern, color);
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

<EditText
<com.amrdeveloper.codeview.CodeView
android:id="@+id/code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="gg">#B57AFF</color>
</resources>
1 change: 1 addition & 0 deletions codeview/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
29 changes: 29 additions & 0 deletions codeview/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'com.android.library'

android {
compileSdk 34

defaultConfig {
minSdkVersion 15
targetSdk 34

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
namespace 'com.amrdeveloper.codeview'
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.7.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
Empty file added codeview/consumer-rules.pro
Empty file.
Loading

0 comments on commit 4ae8b0c

Please sign in to comment.