Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line numbers support #237

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
61 changes: 41 additions & 20 deletions src/main/java/org/billthefarmer/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.support.v4.content.FileProvider;
import android.text.Editable;
import android.text.Html;
import android.text.InputType;
Expand All @@ -67,7 +68,6 @@
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
Expand All @@ -81,12 +81,10 @@
import android.widget.SeekBar;
import android.widget.TextView;

import android.support.v4.content.FileProvider;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;

import org.commonmark.node.*;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

Expand All @@ -96,30 +94,21 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;

import java.lang.ref.WeakReference;

import java.nio.charset.Charset;

import java.text.DateFormat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

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

Expand All @@ -146,6 +135,7 @@ public class Editor extends Activity
public final static String PREF_THEME = "pref_theme";
public final static String PREF_TYPE = "pref_type";
public final static String PREF_WRAP = "pref_wrap";
public final static String PREF_LINE_NUMBERS = "pref_line_numbers";

public final static String DOCUMENTS = "Documents";
public final static String FOLDER = "Folder";
Expand Down Expand Up @@ -428,6 +418,7 @@ public class Editor extends Activity
private MenuItem searchItem;
private SearchView searchView;
private ScrollView scrollView;
private LineNumbersTextView lineNumbersView;
private Runnable updateHighlight;
private Runnable updateWordCount;

Expand All @@ -445,6 +436,7 @@ public class Editor extends Activity
private boolean view = false;

private boolean wrap = false;
private boolean lineNumbers = false;
private boolean suggest = true;

private boolean changed = false;
Expand All @@ -456,6 +448,8 @@ public class Editor extends Activity
private int type = MONO;

private int syntax;
private long highlightRefreshTime = 0; // Syntax highlighting refresh time on scroll changed
private long lineNumbersRefreshTime = 0; // Line numbers refresh time on scroll changed

// onCreate
@Override
Expand All @@ -474,6 +468,7 @@ protected void onCreate(Bundle savedInstanceState)
view = preferences.getBoolean(PREF_VIEW, true);
last = preferences.getBoolean(PREF_LAST, false);
wrap = preferences.getBoolean(PREF_WRAP, false);
lineNumbers = preferences.getBoolean(PREF_LINE_NUMBERS, false);
suggest = preferences.getBoolean(PREF_SUGGEST, true);
highlight = preferences.getBoolean(PREF_HIGH, false);

Expand Down Expand Up @@ -561,6 +556,10 @@ else if (!suggest)

setSizeAndTypeface(size, type);

lineNumbersView = findViewById(R.id.lineNumbersView);
lineNumbersView.setEditText(textView);
lineNumbersView.setLineNumbersEnabled(lineNumbers);

Intent intent = getIntent();
Uri uri = intent.getData();

Expand Down Expand Up @@ -755,8 +754,15 @@ public void onTextChanged(CharSequence s,
// onScrollChange
scrollView.getViewTreeObserver().addOnScrollChangedListener(() ->
{
if (updateHighlight != null)
{
final long time = System.currentTimeMillis();

if (lineNumbers && time - lineNumbersRefreshTime > 125) {
lineNumbersRefreshTime = time;
lineNumbersView.forceRefresh();
}

if (updateHighlight != null && time - highlightRefreshTime > 125) {
highlightRefreshTime = time;
textView.removeCallbacks(updateHighlight);
textView.postDelayed(updateHighlight, UPDATE_DELAY);
}
Expand Down Expand Up @@ -826,6 +832,7 @@ public void onPause()
editor.putBoolean(PREF_VIEW, view);
editor.putBoolean(PREF_LAST, last);
editor.putBoolean(PREF_WRAP, wrap);
editor.putBoolean(PREF_LINE_NUMBERS, lineNumbers);
editor.putBoolean(PREF_SUGGEST, suggest);
editor.putBoolean(PREF_HIGH, highlight);

Expand Down Expand Up @@ -907,6 +914,7 @@ public boolean onPrepareOptionsMenu(Menu menu)
menu.findItem(R.id.openLast).setChecked(last);
menu.findItem(R.id.autoSave).setChecked(save);
menu.findItem(R.id.wrap).setChecked(wrap);
menu.findItem(R.id.lineNumbers).setChecked(lineNumbers);
menu.findItem(R.id.suggest).setChecked(suggest);
menu.findItem(R.id.highlight).setChecked(highlight);

Expand Down Expand Up @@ -1080,6 +1088,9 @@ public boolean onOptionsItemSelected(MenuItem item)
case R.id.wrap:
wrapClicked(item);
break;
case R.id.lineNumbers:
lineNumbersClicked(item);
break;
case R.id.suggest:
suggestClicked(item);
break;
Expand Down Expand Up @@ -1766,14 +1777,17 @@ public void onProgressChanged(SeekBar seekBar,
{
if (fromUser)
listener.onProgressChanged(seekBar, progress);

if (lineNumbers) {
lineNumbersView.forceRefresh();
}
}

@Override
public void onStartTrackingTouch (SeekBar seekBar) {}

@Override
public void onStopTrackingTouch (SeekBar seekBar)
{
public void onStopTrackingTouch (SeekBar seekBar) {
dialog.dismiss();
}
});
Expand Down Expand Up @@ -1815,8 +1829,7 @@ public void onPageFinished(WebView view, String url)
}
});

String htmlDocument =
HTML_HEAD + Html.toHtml(textView.getText()) + HTML_TAIL;
String htmlDocument = HTML_HEAD + Html.toHtml(textView.getText()) + HTML_TAIL;
webView.loadData(htmlDocument, TEXT_HTML, UTF_8);
}

Expand Down Expand Up @@ -1894,6 +1907,14 @@ private void wrapClicked(MenuItem item)
recreate(this);
}

// lineNumbersClicked
private void lineNumbersClicked(MenuItem item)
{
lineNumbers = !lineNumbers;
item.setChecked(lineNumbers);
lineNumbersView.setLineNumbersEnabled(lineNumbers);
}

// suggestClicked
private void suggestClicked(MenuItem item)
{
Expand Down Expand Up @@ -3562,7 +3583,7 @@ protected List<File> doInBackground(String... params)
File entry = new File(path);
entries.add(entry);
}

// Check the entries
for (File file : entries)
{
Expand Down
Loading