Skip to content

Commit

Permalink
Fixed bugs in scaling, rotating, lexing and etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
RainbowC0 committed Jul 7, 2024
1 parent afcdc7c commit e254870
Show file tree
Hide file tree
Showing 9 changed files with 1,570 additions and 1,483 deletions.
92 changes: 72 additions & 20 deletions app/src/main/java/cn/rbc/termuc/EditFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import cn.rbc.codeeditor.util.*;
import cn.rbc.codeeditor.lang.c.*;
import cn.rbc.codeeditor.view.*;
import android.content.*;

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

public EditFragment() {
changes = new ArrayList<>();
}

public EditFragment(String path, int type) {
changes = new ArrayList<>();
Bundle bd = new Bundle();
bd.putString(FL, path);
bd.putInt(TP, type);
Expand All @@ -44,9 +46,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
if (arg == null)
return null;
fl = new File(arg.getString(FL));
lastModified = fl.lastModified();
type = arg.getInt(TP);
final MainActivity ma = (MainActivity)getActivity();
TextEditor editor = new TextEditor(ma);
ed = editor;
editor.setVerticalScrollBarEnabled(true);
if (Settings.mDarkMode)
editor.setColorScheme(ColorSchemeDark.getInstance());
Expand All @@ -55,16 +59,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
editor.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
try {
FileReader fr = new FileReader(fl);
char[] cs = new char[1024];
int i;
StringBuilder sb = new StringBuilder();
while ((i=fr.read(cs)) != -1)
sb.append(cs, 0, i);
fr.close();
String s = sb.toString();
editor.setText(s);
editor.getText().setOnTextChangeListener(this);
String s = load();
if ((type&1) == 0) {
C = "clang";
editor.setLanguage(CLanguage.getInstance());
Expand All @@ -73,22 +68,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
editor.setLanguage(CppLanguage.getInstance());
}
ma.setEditor(editor);
ma.lsp.didOpen(fl, (type&1)==1?"cpp":"c", s);
MainActivity.lsp.didOpen(fl, (type&1)==1?"cpp":"c", s);
} catch(IOException fnf) {
fnf.printStackTrace();
Toast.makeText(ma, "打开失败!", Toast.LENGTH_SHORT).show();
}
return ed = editor;
return editor;
}

public String getC() {
return C;
}

//private int lastVer = -1;
private ArrayList<Range> changes;

public void onChanged(CharSequence c, int start, int ver, boolean ins, boolean typ) {
public void onChanged(CharSequence c, int start, final int ver, boolean ins, boolean typ) {
Document text = ed.getText();
boolean wordwrap = ed.isWordWrap();
Range range = new Range();
Expand Down Expand Up @@ -119,30 +113,88 @@ public void onChanged(CharSequence c, int start, int ver, boolean ins, boolean t
changes.add(range);
//lastStr = (String)c;
//if (lastVer != ver) {
Lsp lsp = ((MainActivity)getActivity()).lsp;
Lsp lsp = MainActivity.lsp;
lsp.didChange(fl, ver, changes);
//lastStart = s;
//lastVer = ver;
// when inserting text and typing, call for completion
if (ins && typ && c.length()==1)
lsp.completionTry(fl, range.enl, range.enc+1, c.charAt(0));
changes.clear();
ed.postDelayed(new Runnable(){
long sendTime = System.currentTimeMillis();
public void run() {
Lsp lsp = MainActivity.lsp;
if (lsp.lastReceivedTime()<sendTime) {
lsp.didChange(fl, ver, ed.getText().toString());
}
}
}, 1000L);
//}
}

@Override
public void onResume() {
super.onResume();
long mLast = fl.lastModified();
if (mLast>lastModified) {
lastModified = mLast;
new AlertDialog.Builder(getContext())
.setTitle(fl.getName())
.setMessage(getString(R.string.file_modified, fl.getName()))
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, null)
.create().show();
}
}

@Override
public void onClick(DialogInterface diag, int id) {
try {
String s = load();
((MainActivity)getActivity()).lsp.didChange(fl, 0, s);
} catch(IOException ioe) {
ioe.printStackTrace();
}
}

@Override
public void onHiddenChanged(boolean hidden)
{
super.onHiddenChanged(hidden);
if (!hidden) {
((MainActivity)getActivity()).setEditor(ed);
if ((type&1) == 0) // C
if ((type&1) == 0) {// C
ed.setLanguage(CLanguage.getInstance());
else
C = "clang";
} else {
ed.setLanguage(CppLanguage.getInstance());
C = "clang++";
}
}
}

public void save() throws IOException {
FileWriter fileWriter = new FileWriter(fl);
fileWriter.write(ed.getText().toString());
fileWriter.close();
lastModified = fl.lastModified();
}

public String load() throws IOException {
FileReader fr = new FileReader(fl);
char[] buf = new char[1024];
int i;
StringBuilder sb = new StringBuilder();
while ((i=fr.read(buf))!=-1)
sb.append(buf, 0, i);
fr.close();
String s = sb.toString();
ed.setText(s);
ed.getText().setOnTextChangeListener(this);
return s;
}

public File getFile() {
return fl;
}
Expand Down
53 changes: 28 additions & 25 deletions app/src/main/java/cn/rbc/termuc/Lsp.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@
import android.util.JsonReader;

public class Lsp {
//final static String METHOD = "method";
//final static String PARAMS = "params";
final static int INITIALIZE = 0, INITIALIZED = 1,
OPEN = 2, CLOSE = 3,
COMPLETION = 4, FIX = 5, CHANGE = 6, SAVE = 7, NOTI = 8;
private final static String TAG = "LSP";
//private static int idx;
private int tp;
int id = 0;
private Socket sk;
private char[] compTrigs = {};
private static SendThread mLastSend;
private long mLastReceivedTime;

public void start(final Context c, final Handler read) {
Utils.run(c, "/system/bin/nc", new String[]{"-l", "-s", "127.0.0.1", "-p", "48455", "clangd", "--header-insertion-decorators=0", "--log=verbose"}, Environment.getExternalStorageDirectory().getAbsolutePath(), true);
public void start(final Context mC, final Handler read) {
Utils.run(mC, "/system/bin/nc", new String[]{"-l", "-s", "127.0.0.1", "-p", "48455", "clangd", "--header-insertion-decorators=0", "--log=verbose"}, Environment.getExternalStorageDirectory().getAbsolutePath(), true);
sk = new Socket();
new HandlerThread("read"){
new Thread(){
public void run() {
try{
int i=0;
Expand All @@ -43,31 +40,25 @@ public void run() {
if (i==20)
throw new IOException("Connection failed");
InputStream is = sk.getInputStream();
final int L = 2048;
byte[] b = new byte[L];
byte[] b = new byte[16];
OUTER: while (true) {
for (i=0;i<16;i++) {
int t = is.read();
if (t==-1)
break OUTER;
b[i] = (byte)t;
}
if (new String(b, 0, 14).equals("Content-Length")) {
if (Arrays.equals(b, "Content-Length: ".getBytes())) {
int len = 0;
while (Character.isDigit(i = is.read()))
len = len * 10 + i - 48;
mLastReceivedTime = System.currentTimeMillis();
is.skip(3L);
StringBuilder sb = new StringBuilder();
while (len > L) {
int rl = is.read(b);
len -= rl;
String s = new String(b, 0, rl, StandardCharsets.UTF_8);
sb.append(s);
}
byte[] strb = new byte[len];
for (i=0; i<len; i++)
b[i] = (byte)is.read();
String s = sb.append(new String(b,0,len,StandardCharsets.UTF_8)).toString();
JsonReader limitInput = new JsonReader(new CharSeqReader(s));
strb[i] = (byte)is.read();
InputStream r = new ByteArrayInputStream(strb);
JsonReader limitInput = new JsonReader(new InputStreamReader(r, StandardCharsets.UTF_8));
Message msg = new Message();
msg.what = tp;
msg.obj = limitInput;
Expand All @@ -84,6 +75,10 @@ public void run() {
}.start();
}

public long lastReceivedTime() {
return mLastReceivedTime;
}

private static String wrap(String m, Object p, boolean req) {
StringBuilder s = new StringBuilder("{\"jsonrpc\":\"2.0\"");
if (req)
Expand Down Expand Up @@ -147,6 +142,17 @@ public void didSave(File f) {
new SendThread("textDocument/didSave", s, false).start();
}

public synchronized void didChange(File f, int version, String text) {
String sb = new StringBuilder("{\"textDocument\":{\"uri\":")
.append(JSONObject.quote(Uri.fromFile(f).toString()))
.append(",\"version\":")
.append(version)
.append("},\"contentChanges\":[{\"text\":")
.append(JSONObject.quote(text))
.append("}]}").toString();
new SendThread("textDocument/didChange", sb, false).start();
}

public synchronized void didChange(File f, int version, List<Range> chs) {
StringBuilder sb = new StringBuilder("{\"textDocument\":{\"uri\":")
.append(JSONObject.quote(Uri.fromFile(f).toString()))
Expand All @@ -171,11 +177,7 @@ public synchronized void didChange(File f, int version, List<Range> chs) {
sb.append('}');
Log.d(TAG, sb.toString());
//tp = CHANGE;
Thread td = new SendThread("textDocument/didChange", sb.toString(), false);
td.start();
/*try{
td.join();
}catch(InterruptedException ie){}*/
new SendThread("textDocument/didChange", sb.toString(), false).start();
}

public synchronized boolean completionTry(File f, int l, int c, char tgc) {
Expand Down Expand Up @@ -227,6 +229,7 @@ public void run() {
else if (!sk.isConnected())
sk.connect(new InetSocketAddress("127.0.0.1", 48455));
OutputStream ow = sk.getOutputStream();
ow.flush();
ow.write(new StringBuilder("Content-Length: ").append(s.length).append("\r\n\r\n").toString().getBytes());
ow.write(s);
ow.flush();
Expand Down
33 changes: 14 additions & 19 deletions app/src/main/java/cn/rbc/termuc/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import cn.rbc.codeeditor.util.*;
import android.preference.*;
import android.content.pm.*;
import android.*;

public class MainActivity extends Activity implements
ActionBar.OnNavigationListener, OnGlobalLayoutListener,
Expand All @@ -40,12 +39,17 @@ public class MainActivity extends Activity implements
private Menu _appmenu;
private ActionBar ab;
private SearchAction mSearchAction;
private Handler hand = new MainHandler(this);
public Lsp lsp;
private static MainHandler hand;
public static Lsp lsp;

private void envInit() {
pwd = new File(getPreferences(MODE_PRIVATE).getString("pwd", root.getPath()));
lsp = new Lsp();
if (lsp==null) {
hand = new MainHandler(this);
lsp = new Lsp();
lsp.start(this, hand);
} else
hand.updateActivity(this);
Settings.getInstance(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));
}

Expand Down Expand Up @@ -83,8 +87,8 @@ protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.activity_main);
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
}, PackageManager.PERMISSION_GRANTED);
showlist = findViewById(R.id.show_list);
keys = findViewById(R.id.keys);
Expand All @@ -100,7 +104,6 @@ protected void onCreate(Bundle savedInstanceState) {
l.setOnItemLongClickListener(this);
refresh();
mSearchAction = new SearchAction(this);
lsp.start(this, hand);
}

private void refresh() {
Expand Down Expand Up @@ -213,7 +216,8 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.run:
try {
save();
lastFrag.save();
lsp.didSave(lastFrag.getFile());
Utils.run(this, new StringBuffer(Utils.PREF).append("/usr/bin/bash").toString(), new String[]{"-c",
new StringBuffer(lastFrag.getC())
.append(" \"").append(escape(lastFrag.getFile().getAbsolutePath())).append("\" ")
Expand All @@ -231,7 +235,8 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
break;
case R.id.save:
try {
save();
lastFrag.save();
lsp.didSave(lastFrag.getFile());
toast("已保存");
} catch(IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -265,8 +270,6 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
Intent it = new Intent(this, SettingsActivity.class);
startActivity(it);
break;
case R.id.test:
break;
}
return true;
}
Expand Down Expand Up @@ -359,14 +362,6 @@ public void createFile(View view) {
.create().show();
}

public void save() throws IOException {
File f = lastFrag.getFile();
FileWriter fileWriter = new FileWriter(f);
fileWriter.write(codeEditor.getText().toString());
fileWriter.close();
lsp.didSave(f);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/cn/rbc/termuc/MainHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class MainHandler extends Handler implements Comparator<ErrSpan> {
TG = "triggerCharacters";

public MainHandler(MainActivity ma) {
super();
this.ma = ma;
}

public void updateActivity(MainActivity ma) {
this.ma = ma;
}

Expand Down
Loading

0 comments on commit e254870

Please sign in to comment.