Skip to content

Commit

Permalink
Supported formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
RainbowC0 committed Jul 19, 2024
1 parent 7c50aeb commit b717198
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This app use `com.termux.RUN_COMMAND` to call Termux to run command, and run `cl

- [x] Highighting
- [x] Autocompletion
- [x] Formatting
- [x] Diagnose
- [x] Compile flags
- [x] Dark mode
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TermuC 是一个简单的 C/C++ IDE,采用 Termux 作为后台。项目基于

- [x] 代码高亮
- [x] 自动补全
- [x] 格式化
- [x] 代码诊断
- [x] 编译选项
- [x] 暗主题
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionName "0.1"
}

buildTypes {
Expand Down
38 changes: 35 additions & 3 deletions app/src/main/java/cn/rbc/termuc/EditFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.widget.*;
import java.io.*;
import android.net.*;
import java.util.*;
import cn.rbc.codeeditor.lang.*;
import cn.rbc.codeeditor.common.*;
import cn.rbc.codeeditor.util.*;
Expand All @@ -14,7 +13,7 @@
import android.content.*;

public class EditFragment extends Fragment
implements OnTextChangeListener, DialogInterface.OnClickListener
implements OnTextChangeListener, DialogInterface.OnClickListener, Formatter
{
public final static int TYPE_C = 0;
public final static int TYPE_CPP = 1;
Expand All @@ -26,7 +25,7 @@ public class EditFragment extends Fragment
int type;
private String C = "clang";
private long lastModified;
private List<Range> changes = new ArrayList<>();
private java.util.List<Range> changes = new java.util.ArrayList<>();

public EditFragment() {
}
Expand Down Expand Up @@ -58,6 +57,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
editor.setShowNonPrinting(Settings.whitespace);
editor.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
if ("s".equals(Settings.completion))
editor.setFormatter(this);
try {
String s = load();
if ((type&1) == 0) {
Expand All @@ -80,6 +81,37 @@ public String getC() {
return C;
}

@Override
public int createAutoIndent(CharSequence text) {
return 0;
}

@Override
public CharSequence format(CharSequence txt, int width) {
int start = ed.getSelectionStart(), end = ed.getSelectionEnd();
if (start==end)
MainActivity.lsp.formatting(fl, width);
else {
Range range = new Range();
Document text = ed.getText();
if (ed.isWordWrap()) {
range.stl = text.findLineNumber(start);
range.stc = text.getLineOffset(range.stl);
range.enl = text.findLineNumber(end);
range.enc = text.getLineOffset(range.enl);
} else {
range.stl = text.findRowNumber(start);
range.stc = text.getRowOffset(range.stl);
range.enl = text.findRowNumber(start);
range.enc = text.getRowOffset(range.enl);
}
range.stc = start - range.stc;
range.enc = end - range.enc;
MainActivity.lsp.rangeFormatting(fl, range, width);
}
return null;
}

//private int lastVer = -1;

public void onChanged(CharSequence c, int start, final int ver, boolean ins, boolean typ) {
Expand Down
32 changes: 31 additions & 1 deletion app/src/main/java/cn/rbc/termuc/Lsp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Lsp {
private long mLastReceivedTime;

public void start(final Context mC, final Handler read) {
Utils.run(mC, "/system/bin/nc", new String[]{"-l", "-s", Settings.lsp_host, "-p", Integer.toString(Settings.lsp_port), "clangd", "--header-insertion-decorators=0", "--completion-style=bundled"}, Environment.getExternalStorageDirectory().getAbsolutePath(), true);
Utils.run(mC, "/system/bin/nc", new String[]{"-l", "-s", Settings.lsp_host, "-p", Integer.toString(Settings.lsp_port), "clangd", "--header-insertion-decorators=0", "--log=verbose", "--completion-style=bundled"}, Environment.getExternalStorageDirectory().getAbsolutePath(), true);
sk = new Socket();
mExecutor = Executors.newSingleThreadExecutor();
new Thread(){
Expand Down Expand Up @@ -60,6 +60,8 @@ public void run() {
byte[] strb = new byte[len];
for (i=0; i<len; i++)
strb[i] = (byte)is.read();
if (cn.rbc.termuc.BuildConfig.DEBUG)
Log.i(TAG, new String(strb));
InputStream r = new ByteArrayInputStream(strb);
JsonReader limitInput = new JsonReader(new InputStreamReader(r, StandardCharsets.UTF_8));
Message msg = new Message();
Expand Down Expand Up @@ -207,6 +209,34 @@ public synchronized boolean completionTry(File f, int l, int c, char tgc) {
return true;
}

public synchronized void formatting(File fl, int tabSize) {
StringBuilder sb = new StringBuilder("{\"textDocument\":{\"uri\":")
.append(JSONObject.quote(Uri.fromFile(fl).toString()))
.append("},\"options\":{\"tabSize\":")
.append(tabSize)
.append(",\"insertSpaces\":true}}");
Log.d(TAG, sb.toString());
mExecutor.execute(new Send("textDocument/formatting", sb.toString(), true));
}

public synchronized void rangeFormatting(File fl, Range range, int tabSize) {
StringBuilder sb = new StringBuilder("{\"textDocument\":{\"uri\":")
.append(JSONObject.quote(Uri.fromFile(fl).toString()))
.append("},\"range\":{\"start\":{\"line\":")
.append(range.stl)
.append(",\"character\":")
.append(range.stc)
.append("},\"end\":{\"line\":")
.append(range.enl)
.append(",\"character\":")
.append(range.enc)
.append("}},\"options\":{\"tabSize\":")
.append(tabSize)
.append(",\"insertSpaces\":true}}");
Log.d(TAG, sb.toString());
mExecutor.execute(new Send("textDocument/rangeFormatting", sb.toString(), true));
}

public boolean isConnected() {
return sk.isConnected();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/cn/rbc/termuc/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ protected void onResume() {
TextEditor ed = (TextEditor)f.getView();
ed.setWordWrap(Settings.wordwrap);
ed.setShowNonPrinting(Settings.whitespace);
ed.setFormatter("s".equals(Settings.completion)?(EditFragment)f:null);
}
}

Expand Down
41 changes: 38 additions & 3 deletions app/src/main/java/cn/rbc/termuc/MainHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ public void handleMessage(Message msg) {
int sl = 0, sc = 0, el = 0, ec = 0;
Object tmp1 = null, tmp2 = null, tmp3 = null;
LOOP: while (true) {
Log.i("LSP", stack.toString());
switch (jr.peek()) {
case NAME:
String n = jr.nextName();
switch (n) {
case NEWTX:
n = jr.nextString();
//if (tmp3 instanceof Edit)
((Edit)tmp3).text = jr.nextString();
((Edit)tmp3).text = n;
//else tmp3 = n;
break;
case LABEL:
if (tmp2 instanceof ListItem)
Expand Down Expand Up @@ -116,12 +119,20 @@ public void handleMessage(Message msg) {
}
jr.endObject();
break;
case RESU:
if (jr.peek()==BEGIN_ARRAY) {
jr.beginArray();
tmp2 = new ArrayList<Edit>();
//tmpi = ma.getEditor().getCaretPosition();
} else
jr.beginObject();
stack.push(n);
break;
case TEDIT:
tmp3 = new Edit();
case COMPLE:
case CAPA:
case PARA:
case RESU:
jr.beginObject();
stack.push(n);
break;
Expand All @@ -135,6 +146,7 @@ public void handleMessage(Message msg) {
if (!stack.isEmpty()) {
switch (stack.peek()) {
case ADDEDIT:
case RESU:
tmp3 = new Edit();
break;
case IT:
Expand All @@ -151,11 +163,15 @@ public void handleMessage(Message msg) {
if (!stack.isEmpty())
switch (stack.peek()) {
case ADDEDIT:
case RESU:
Edit _p = (Edit)tmp3;
Document te = ma.getEditor().getText();
_p.start = te.getLineOffset(sl) + sc;
_p.len = te.getLineOffset(el) + ec - _p.start;
((ListItem)tmp2).edits.addLast(_p);
if (tmp2 instanceof ListItem)
((ListItem)tmp2).edits.addLast(_p);
else
((List)tmp2).add(0, _p);
break;
case TEDIT:
_p = (Edit)tmp3;
Expand Down Expand Up @@ -200,6 +216,25 @@ public void handleMessage(Message msg) {
te.getText().setDiag(a);
te.invalidate();
break LOOP;
case RESU:
jr.close();
te = ma.getEditor();
Document doc = te.getText();
doc.beginBatchEdit();
long tpl = System.nanoTime();
int mc = te.getCaretPosition();
for (Edit e:(List<Edit>)tmp2) {
doc.deleteAt(e.start, e.len, tpl);
doc.insertBefore(e.text.toCharArray(), e.start, tpl);
if (e.start + e.len <= mc)
mc += e.text.length() - e.len;
else if (e.start < mc)
mc = e.start + e.text.length();
}
doc.endBatchEdit();
te.moveCaret(mc);
te.mCtrlr.determineSpans();
break LOOP;
}
break;
case END_DOCUMENT:
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/cn/rbc/termuc/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class TextEditor extends FreeScrollingTextField{
private Context mContext;
private String _lastSelectFile;
private int _index;
//private int mSize = 14;
private Formatter mFormatter;

/*
private Handler handler = new Handler() {
@Override
Expand Down Expand Up @@ -126,6 +127,18 @@ public boolean onKeyShortcut(int keyCode, KeyEvent event) {
return super.onKeyShortcut(keyCode, event);
}

@Override
public void format() {
if (mFormatter!=null) {
mFormatter.format(hDoc, mAutoIndentWidth);
} else
super.format();
}

public void setFormatter(Formatter fmt) {
mFormatter = fmt;
}

public void setText(CharSequence c) {
Document doc = new Document(this);
doc.setWordWrap(isWordWrap());
Expand Down
2 changes: 1 addition & 1 deletion codeeditor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {

buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public LexerTokenizer getTokenizer() {
}*/

//子类需要实现该方法
public DefFormatter getFormatter() {
public Formatter getFormatter() {
return DefFormatter.getInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CLanguage extends Language{
"signed", "unsigned", "sizeof", "typedef",
"enum", "struct", "union",
"break", "case", "continue", "default", "do", "else", "for",
"goto", "if", "return", "switch", "while"*
"goto", "if", "return", "switch", "while"
};*/

public static Language getInstance(){
Expand All @@ -31,5 +31,10 @@ public boolean isProgLang() {
public Lexer newLexer(CharSeqReader reader) {
return new CLexer(reader);
}

@Override
public Formatter getFormatter() {
return super.getFormatter();
}
}

Loading

0 comments on commit b717198

Please sign in to comment.