opt: use my code completion implement instead CodeMirror's
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.stardust.scriptdroid.model.indices.Module;
|
||||
import com.stardust.scriptdroid.model.indices.Modules;
|
||||
import com.stardust.scriptdroid.model.indices.Property;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/3.
|
||||
*/
|
||||
|
||||
public class AutoCompletion {
|
||||
|
||||
public interface AutoCompleteCallback {
|
||||
|
||||
void updateCodeCompletion(CodeCompletions codeCompletions);
|
||||
}
|
||||
|
||||
private static final Pattern STATEMENT = Pattern.compile("([A-Za-z]+\\.)?([a-zA-Z][a-zA-Z0-9_]*)?$");
|
||||
|
||||
private String mModuleName;
|
||||
private String mPropertyPrefill;
|
||||
private List<Module> mModules;
|
||||
private DictionaryTree<Property> mGlobalPropertyTree = new DictionaryTree<>();
|
||||
private AutoCompleteCallback mAutoCompleteCallback;
|
||||
|
||||
public AutoCompletion(Context context) {
|
||||
buildDictionaryTree(context);
|
||||
}
|
||||
|
||||
public void setAutoCompleteCallback(AutoCompleteCallback autoCompleteCallback) {
|
||||
mAutoCompleteCallback = autoCompleteCallback;
|
||||
}
|
||||
|
||||
private void buildDictionaryTree(Context context) {
|
||||
Modules.getInstance().getModules(context)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnNext(this::buildDictionaryTree)
|
||||
.subscribe(modules -> mModules = modules);
|
||||
}
|
||||
|
||||
private void buildDictionaryTree(List<Module> modules) {
|
||||
for (Module module : modules) {
|
||||
mGlobalPropertyTree.putWord(module.getName(), module.asGlobalProperty());
|
||||
for (Property property : module.getProperties()) {
|
||||
if (property.isGlobal())
|
||||
mGlobalPropertyTree.putWord(property.getKey(), property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onCursorChange(String line, int cursor) {
|
||||
if (cursor <= 0 || line == null || line.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (mModules == null || mAutoCompleteCallback == null)
|
||||
return;
|
||||
findStatementOnCursor(line, cursor);
|
||||
if (mPropertyPrefill == null && mModuleName == null)
|
||||
return;
|
||||
Module module = getModule(mModuleName);
|
||||
List<CodeCompletion> completions = findCodeCompletion(module, mPropertyPrefill);
|
||||
CodeCompletions codeCompletions = new CodeCompletions(cursor, completions);
|
||||
mAutoCompleteCallback.updateCodeCompletion(codeCompletions);
|
||||
}
|
||||
|
||||
private Module getModule(String moduleName) {
|
||||
if (moduleName == null)
|
||||
return null;
|
||||
for (Module module : mModules) {
|
||||
if (module.getName().equals(moduleName)) {
|
||||
return module;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void findStatementOnCursor(String line, int cursor) {
|
||||
Matcher matcher = STATEMENT.matcher(line.substring(0, cursor));
|
||||
if (!matcher.find()) {
|
||||
mModuleName = mPropertyPrefill = null;
|
||||
return;
|
||||
}
|
||||
if (matcher.groupCount() == 2) {
|
||||
String module = matcher.group(1);
|
||||
mModuleName = module == null ? null : module.substring(0, module.length() - 1);
|
||||
mPropertyPrefill = matcher.group(2);
|
||||
} else {
|
||||
mModuleName = null;
|
||||
mPropertyPrefill = matcher.group(1);
|
||||
}
|
||||
}
|
||||
|
||||
private List<CodeCompletion> findCodeCompletion(Module module, String propertyPrefill) {
|
||||
if (module == null)
|
||||
return findCodeCompletionForGlobal(propertyPrefill);
|
||||
return findCodeCompletionForModule(module, propertyPrefill);
|
||||
}
|
||||
|
||||
private List<CodeCompletion> findCodeCompletionForModule(Module module, String propertyPrefill) {
|
||||
List<CodeCompletion> completions = new ArrayList<>();
|
||||
int len = propertyPrefill == null ? 0 : propertyPrefill.length();
|
||||
for (Property property : module.getProperties()) {
|
||||
if (propertyPrefill == null || property.getKey().startsWith(propertyPrefill)) {
|
||||
completions.add(new CodeCompletion(property.getKey(), property.getUrl(), len));
|
||||
}
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
|
||||
private List<CodeCompletion> findCodeCompletionForGlobal(String propertyPrefill) {
|
||||
List<DictionaryTree.Entry<Property>> result = mGlobalPropertyTree.searchByPrefill(propertyPrefill);
|
||||
List<CodeCompletion> completions = new ArrayList<>();
|
||||
for (DictionaryTree.Entry<Property> entry : result) {
|
||||
Property property = entry.tag;
|
||||
completions.add(new CodeCompletion(property.getKey(), property.getUrl(), propertyPrefill.length()));
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/3.
|
||||
*/
|
||||
|
||||
public class CodeCompletion {
|
||||
|
||||
private final String mHint;
|
||||
private final String mUrl;
|
||||
private final String mInsertText;
|
||||
private final int mInsertPos;
|
||||
|
||||
public CodeCompletion(String hint, String url, int insertPos) {
|
||||
mHint = hint;
|
||||
mUrl = url;
|
||||
mInsertPos = insertPos;
|
||||
mInsertText = null;
|
||||
}
|
||||
|
||||
public CodeCompletion(String hint, String url, String insertText) {
|
||||
mHint = hint;
|
||||
mUrl = url;
|
||||
mInsertText = insertText;
|
||||
mInsertPos = -1;
|
||||
}
|
||||
|
||||
public String getHint() {
|
||||
return mHint;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return mUrl;
|
||||
}
|
||||
|
||||
public String getInsertText() {
|
||||
if (mInsertText != null)
|
||||
return mInsertText;
|
||||
if (mInsertPos == 0) {
|
||||
return mHint;
|
||||
}
|
||||
return mHint.substring(mInsertPos);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/27.
|
||||
*/
|
||||
public class CodeCompletions {
|
||||
|
||||
|
||||
private int mFrom;
|
||||
private List<CodeCompletion> mCompletions;
|
||||
|
||||
public CodeCompletions(int cursor, List<CodeCompletion> completions) {
|
||||
mFrom = cursor;
|
||||
mCompletions = completions;
|
||||
}
|
||||
|
||||
public static CodeCompletions just(List<String> hints) {
|
||||
List<CodeCompletion> completions = new ArrayList<>(hints.size());
|
||||
for (String hint : hints) {
|
||||
completions.add(new CodeCompletion(hint, null, 0));
|
||||
}
|
||||
return new CodeCompletions(-1, completions);
|
||||
}
|
||||
|
||||
public int getFrom() {
|
||||
return mFrom;
|
||||
}
|
||||
|
||||
|
||||
public int size() {
|
||||
return mCompletions.size();
|
||||
}
|
||||
|
||||
public String getHint(int position) {
|
||||
return mCompletions.get(position).getHint();
|
||||
}
|
||||
|
||||
public CodeCompletion get(int pos) {
|
||||
return mCompletions.get(pos);
|
||||
}
|
||||
|
||||
public String getUrl(int pos) {
|
||||
return mCompletions.get(pos).getUrl();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/3.
|
||||
*/
|
||||
|
||||
public class DictionaryTree<T> {
|
||||
|
||||
private static class Node<T> {
|
||||
|
||||
char ch;
|
||||
Map<Character, Node<T>> children = new TreeMap<>();
|
||||
String wordEndHere;
|
||||
T tag;
|
||||
|
||||
Node(char ch) {
|
||||
this.ch = ch;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Entry<T> {
|
||||
public String word;
|
||||
public T tag;
|
||||
|
||||
public Entry(String word, T tag) {
|
||||
this.word = word;
|
||||
this.tag = tag;
|
||||
}
|
||||
}
|
||||
|
||||
private Node<T> mRoot = new Node<>('@');
|
||||
|
||||
public void putWord(String word, T tag) {
|
||||
Node<T> node = mRoot;
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
node = getOrCreateNode(node, word.charAt(i));
|
||||
}
|
||||
node.tag = tag;
|
||||
node.wordEndHere = word;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public List<Entry<T>> searchByPrefill(String prefill) {
|
||||
Node<T> node = mRoot;
|
||||
for (int i = 0; i < prefill.length(); i++) {
|
||||
node = node.children.get(prefill.charAt(i));
|
||||
if (node == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
List<Entry<T>> entries = new ArrayList<>();
|
||||
collectChildren(node, entries);
|
||||
return entries;
|
||||
}
|
||||
|
||||
private void collectChildren(Node<T> node, List<Entry<T>> entries) {
|
||||
for (Map.Entry<Character, Node<T>> entry : node.children.entrySet()) {
|
||||
Node<T> child = entry.getValue();
|
||||
if (child.wordEndHere != null) {
|
||||
entries.add(new Entry<>(child.wordEndHere, child.tag));
|
||||
}
|
||||
collectChildren(child, entries);
|
||||
}
|
||||
}
|
||||
|
||||
private Node<T> getOrCreateNode(Node<T> parent, char ch) {
|
||||
Node<T> child = parent.children.get(ch);
|
||||
if (child == null) {
|
||||
child = new Node<>(ch);
|
||||
parent.children.put(ch, child);
|
||||
}
|
||||
return child;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.stardust.scriptdroid.ui.edit.completion;
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.util.List;
|
||||
public class Module {
|
||||
|
||||
@SerializedName("properties")
|
||||
|
||||
private List<Property> mProperties = new ArrayList<>();
|
||||
|
||||
@SerializedName("url")
|
||||
@@ -53,4 +52,8 @@ public class Module {
|
||||
public void setSummary(String summary) {
|
||||
mSummary = summary;
|
||||
}
|
||||
|
||||
public Property asGlobalProperty() {
|
||||
return new Property(mName, mUrl, mSummary, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,16 @@ public class Property {
|
||||
@SerializedName("global")
|
||||
private boolean mGlobal;
|
||||
|
||||
public Property() {
|
||||
}
|
||||
|
||||
public Property(String key, String url, String summary, boolean global) {
|
||||
mUrl = url;
|
||||
mKey = key;
|
||||
mSummary = summary;
|
||||
mGlobal = global;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return mUrl;
|
||||
}
|
||||
|
||||
@@ -63,9 +63,9 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
|
||||
|
||||
public interface Callback {
|
||||
void onChange();
|
||||
|
||||
void updateCodeCompletion(int fromLine, int fromCh, int toLine, int toCh, String[] list, String[] urls);
|
||||
void onKeyUp(String line, int ch);
|
||||
|
||||
}
|
||||
|
||||
private static String[] sAvailableThemes;
|
||||
@@ -134,17 +134,14 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
}
|
||||
|
||||
public void setTheme(final String theme) {
|
||||
mPageFinished.promise().done(new DoneCallback<Void>() {
|
||||
@Override
|
||||
public void onDone(Void result) {
|
||||
evalJavaScript(String.format(Locale.getDefault(),
|
||||
"var e = document.createElement('link');e.setAttribute('rel', 'stylesheet');" +
|
||||
"e.setAttribute('type', 'text/css');e.setAttribute('href', '%s');" +
|
||||
"document.getElementsByTagName('head')[0].appendChild(e);",
|
||||
"codemirror/theme/" + theme + ".css"));
|
||||
evalJavaScript(String.format(Locale.getDefault(), "editor.setOption('theme', '%s');", theme));
|
||||
mTheme = theme;
|
||||
}
|
||||
mPageFinished.promise().done(result -> {
|
||||
evalJavaScript(String.format(Locale.getDefault(),
|
||||
"var e = document.createElement('link');e.setAttribute('rel', 'stylesheet');" +
|
||||
"e.setAttribute('type', 'text/css');e.setAttribute('href', '%s');" +
|
||||
"document.getElementsByTagName('head')[0].appendChild(e);",
|
||||
"codemirror/theme/" + theme + ".css"));
|
||||
evalJavaScript(String.format(Locale.getDefault(), "editor.setOption('theme', '%s');", theme));
|
||||
mTheme = theme;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -442,19 +439,11 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void onTextChange() {
|
||||
public void onKeyUp(String line, int cursor) {
|
||||
if (mCallback == null) {
|
||||
return;
|
||||
}
|
||||
mWebView.post(() -> mCallback.onChange());
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void updateCodeCompletion(final int fromLine, final int fromCh, final int toLine, final int toCh, final String[] list, final String[] urls) {
|
||||
if (mCallback == null) {
|
||||
return;
|
||||
}
|
||||
mWebView.post(() -> mCallback.updateCodeCompletion(fromLine, fromCh, toLine, toCh, list, urls));
|
||||
mWebView.post(() -> mCallback.onKeyUp(line, cursor));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -534,28 +523,4 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
}
|
||||
}
|
||||
|
||||
private class MyInputConnection extends InputConnectProxy {
|
||||
|
||||
public MyInputConnection(InputConnection inputConnection) {
|
||||
super(inputConnection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendKeyEvent(KeyEvent event) {
|
||||
Log.d(LOG_TAG, "sendKeyEvent: " + event);
|
||||
return super.sendKeyEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean performContextMenuAction(int id) {
|
||||
if (id == android.R.id.selectAll) {
|
||||
post(CodeMirrorEditor.this::selectAll);
|
||||
return true;
|
||||
}
|
||||
if (id == android.R.id.startSelectingText) {
|
||||
evalJavaScript("editor.setSelection(editor.getCursor(), {line: editor.getCursor().line, ch: editor.getCursor().ch - 1});");
|
||||
}
|
||||
return super.performContextMenuAction(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,18 +19,19 @@ import android.widget.ImageView;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.Pref;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.model.autocomplete.AutoCompletion;
|
||||
import com.stardust.scriptdroid.model.autocomplete.CodeCompletion;
|
||||
import com.stardust.scriptdroid.model.indices.Module;
|
||||
import com.stardust.scriptdroid.model.indices.Property;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.ui.doc.ManualDialog;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.CodeCompletions;
|
||||
import com.stardust.scriptdroid.model.autocomplete.CodeCompletions;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.CodeCompletionBar;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.InputMethodEnhancedBarColors;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.Symbols;
|
||||
import com.stardust.scriptdroid.model.autocomplete.Symbols;
|
||||
import com.stardust.scriptdroid.ui.edit.keyboard.FunctionsKeyboardHelper;
|
||||
import com.stardust.scriptdroid.ui.edit.keyboard.FunctionsKeyboardView;
|
||||
import com.stardust.scriptdroid.ui.log.LogActivity_;
|
||||
@@ -101,9 +102,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
private String mName;
|
||||
private File mFile;
|
||||
private boolean mReadOnly = false;
|
||||
|
||||
private ScriptExecution mScriptExecution;
|
||||
private boolean mTextChanged = false;
|
||||
private AutoCompletion mAutoCompletion;
|
||||
private FunctionsKeyboardHelper mFunctionsKeyboardHelper;
|
||||
private BroadcastReceiver mOnRunFinishedReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
@@ -220,34 +221,27 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
mSymbolBar.setCodeCompletions(Symbols.getSymbols());
|
||||
mCodeCompletionBar.setOnHintClickListener(this);
|
||||
mSymbolBar.setOnHintClickListener(this);
|
||||
mAutoCompletion = new AutoCompletion(getContext());
|
||||
mAutoCompletion.setAutoCompleteCallback(mCodeCompletionBar::setCodeCompletions);
|
||||
}
|
||||
|
||||
|
||||
private void setUpEditor() {
|
||||
setTheme(PreferenceManager.getDefaultSharedPreferences(getContext())
|
||||
.getString(KEY_EDITOR_THEME, mEditor.getTheme()));
|
||||
mEditor.setCallback(new CodeMirrorEditor.Callback() {
|
||||
@Override
|
||||
public void onChange() {
|
||||
mTextChanged = true;
|
||||
setMenuItemStatus(R.id.save, true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateCodeCompletion(int fromLine, int fromCh, int toLine, int toCh, final String[] list, final String[] urls) {
|
||||
mCodeCompletionBar.setCodeCompletions(new CodeCompletions(
|
||||
new CodeCompletions.Pos(fromLine, fromCh),
|
||||
new CodeCompletions.Pos(toLine, toCh),
|
||||
Arrays.asList(list),
|
||||
Arrays.asList(urls)
|
||||
));
|
||||
}
|
||||
mEditor.setCallback((line, cursor) -> {
|
||||
mTextChanged = true;
|
||||
setMenuItemStatus(R.id.save, true);
|
||||
autoComplete(line, cursor);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void autoComplete(String line, int cursor) {
|
||||
mAutoCompletion.onCursorChange(line, cursor);
|
||||
}
|
||||
|
||||
public void setTheme(String theme) {
|
||||
mEditor.setTheme(theme);
|
||||
mInputMethodEnhanceBar.setBackgroundColor(InputMethodEnhancedBarColors.getBackgroundColor(theme));
|
||||
@@ -403,21 +397,16 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
|
||||
@Override
|
||||
public void onHintClick(CodeCompletions completions, int pos) {
|
||||
if (completions.shouldBeInserted()) {
|
||||
mEditor.insert(completions.getHints().get(pos));
|
||||
return;
|
||||
}
|
||||
mEditor.replace(completions.getHints().get(pos), completions.getFrom().line, completions.getFrom().ch,
|
||||
completions.getTo().line, completions.getTo().ch);
|
||||
CodeCompletion completion = completions.get(pos);
|
||||
mEditor.insert(completion.getInsertText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHintLongClick(CodeCompletions completions, int pos) {
|
||||
String url = completions.getUrltAt(pos);
|
||||
if (url == null)
|
||||
CodeCompletion completion = completions.get(pos);
|
||||
if (completion.getUrl() == null)
|
||||
return;
|
||||
showManual(url, completions.getHints().get(pos));
|
||||
|
||||
showManual(completion.getUrl(), completion.getHint());
|
||||
}
|
||||
|
||||
private void showManual(String url, String title) {
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.stardust.scriptdroid.ui.edit.completion;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
@@ -11,26 +8,10 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import android.workground.WrapContentLinearLayoutManager;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.tool.GsonUtils;
|
||||
import com.stardust.util.ClipboardUtil;
|
||||
import com.stardust.util.UnderuseExecutors;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.stardust.scriptdroid.model.autocomplete.CodeCompletions;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/2/17.
|
||||
@@ -51,7 +32,7 @@ public class CodeCompletionBar extends RecyclerView {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int position = getChildViewHolder(v).getAdapterPosition();
|
||||
if (position >= 0 && position < mCodeCompletions.getHints().size()) {
|
||||
if (position >= 0 && position < mCodeCompletions.size()) {
|
||||
if (mOnHintClickListener != null) {
|
||||
mOnHintClickListener.onHintClick(mCodeCompletions, position);
|
||||
}
|
||||
@@ -64,7 +45,7 @@ public class CodeCompletionBar extends RecyclerView {
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
int position = getChildViewHolder(v).getAdapterPosition();
|
||||
if (position < 0 || position >= mCodeCompletions.getHints().size())
|
||||
if (position < 0 || position >= mCodeCompletions.size())
|
||||
return false;
|
||||
if (mOnHintClickListener != null) {
|
||||
mOnHintClickListener.onHintLongClick(mCodeCompletions, position);
|
||||
@@ -122,7 +103,7 @@ public class CodeCompletionBar extends RecyclerView {
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
TextView textView = ((TextView) holder.itemView);
|
||||
textView.setText(mCodeCompletions.getHints().get(position));
|
||||
textView.setText(mCodeCompletions.getHint(position));
|
||||
if (mTextColor != 0) {
|
||||
textView.setTextColor(mTextColor);
|
||||
}
|
||||
@@ -130,7 +111,7 @@ public class CodeCompletionBar extends RecyclerView {
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mCodeCompletions == null ? 0 : mCodeCompletions.getHints().size();
|
||||
return mCodeCompletions == null ? 0 : mCodeCompletions.size();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.stardust.scriptdroid.ui.edit.completion;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/27.
|
||||
*/
|
||||
// TODO: 2017/10/24 refactor
|
||||
public class CodeCompletions {
|
||||
|
||||
public static class Pos {
|
||||
public int line;
|
||||
public int ch;
|
||||
|
||||
public Pos(int line, int ch) {
|
||||
this.line = line;
|
||||
this.ch = ch;
|
||||
}
|
||||
}
|
||||
|
||||
private Pos mFrom;
|
||||
private Pos mTo;
|
||||
private List<String> mHints;
|
||||
private List<String> mUrls;
|
||||
|
||||
public CodeCompletions(Pos from, Pos to, List<String> hints, List<String> urls) {
|
||||
mFrom = from;
|
||||
mTo = to;
|
||||
mHints = hints;
|
||||
mUrls = urls;
|
||||
}
|
||||
|
||||
public static CodeCompletions just(List<String> hints) {
|
||||
return new CodeCompletions(null, null, hints, null);
|
||||
}
|
||||
|
||||
public Pos getFrom() {
|
||||
return mFrom;
|
||||
}
|
||||
|
||||
public Pos getTo() {
|
||||
return mTo;
|
||||
}
|
||||
|
||||
public String getUrltAt(int pos) {
|
||||
if (mUrls == null)
|
||||
return null;
|
||||
return mUrls.get(pos);
|
||||
}
|
||||
|
||||
public List<String> getHints() {
|
||||
return mHints;
|
||||
}
|
||||
|
||||
|
||||
public boolean shouldBeInserted() {
|
||||
return mFrom == null && mTo == null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user