feat: any words' completion
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
import android.text.Editable;
|
||||
|
||||
import com.stardust.scriptdroid.model.indices.Property;
|
||||
import com.stardust.scriptdroid.ui.widget.SimpleTextWatcher;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/26.
|
||||
*/
|
||||
|
||||
public class AnyWordsCompletion implements SimpleTextWatcher.AfterTextChangedListener {
|
||||
|
||||
|
||||
private static final String PATTERN = "[\\W]";
|
||||
private ExecutorService mExecutorService;
|
||||
private volatile DictionaryTree<String> mDictionaryTree;
|
||||
|
||||
public AnyWordsCompletion(ExecutorService executorService) {
|
||||
mExecutorService = executorService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
mExecutorService.execute(() -> splitWords(s.toString()));
|
||||
}
|
||||
|
||||
private void splitWords(String s) {
|
||||
DictionaryTree<String> tree = new DictionaryTree<>();
|
||||
String[] words = s.split(PATTERN);
|
||||
for (String word : words) {
|
||||
tree.putWord(word, word);
|
||||
}
|
||||
mDictionaryTree = tree;
|
||||
}
|
||||
|
||||
public void findCodeCompletion(List<CodeCompletion> completions, String wordPrefill) {
|
||||
if (mDictionaryTree == null)
|
||||
return;
|
||||
List<DictionaryTree.Entry<String>> result = mDictionaryTree.searchByPrefill(wordPrefill);
|
||||
for (DictionaryTree.Entry<String> entry : result) {
|
||||
completions.add(new CodeCompletion(entry.tag, null, wordPrefill.length()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
package com.stardust.scriptdroid.model.autocomplete;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.EditText;
|
||||
|
||||
import com.stardust.scriptdroid.model.indices.Module;
|
||||
import com.stardust.scriptdroid.model.indices.Modules;
|
||||
import com.stardust.scriptdroid.model.indices.Property;
|
||||
import com.stardust.scriptdroid.ui.widget.SimpleTextWatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -33,9 +37,13 @@ public class AutoCompletion {
|
||||
private List<Module> mModules;
|
||||
private DictionaryTree<Property> mGlobalPropertyTree = new DictionaryTree<>();
|
||||
private AutoCompleteCallback mAutoCompleteCallback;
|
||||
private ExecutorService mExecutorService = Executors.newSingleThreadExecutor();
|
||||
private AnyWordsCompletion mAnyWordsCompletion;
|
||||
|
||||
public AutoCompletion(Context context) {
|
||||
public AutoCompletion(Context context, EditText editText) {
|
||||
buildDictionaryTree(context);
|
||||
mAnyWordsCompletion = new AnyWordsCompletion(mExecutorService);
|
||||
editText.addTextChangedListener(new SimpleTextWatcher(mAnyWordsCompletion));
|
||||
}
|
||||
|
||||
public void setAutoCompleteCallback(AutoCompleteCallback autoCompleteCallback) {
|
||||
@@ -71,9 +79,13 @@ public class AutoCompletion {
|
||||
Module module = getModule(mModuleName);
|
||||
if (mPropertyPrefill == null && module == null)
|
||||
return;
|
||||
List<CodeCompletion> completions = findCodeCompletion(module, mPropertyPrefill);
|
||||
CodeCompletions codeCompletions = new CodeCompletions(cursor, completions);
|
||||
mAutoCompleteCallback.updateCodeCompletion(codeCompletions);
|
||||
String prefill = mPropertyPrefill;
|
||||
mExecutorService.execute(() -> {
|
||||
List<CodeCompletion> completions = findCodeCompletion(module, prefill);
|
||||
CodeCompletions codeCompletions = new CodeCompletions(cursor, completions);
|
||||
mAutoCompleteCallback.updateCodeCompletion(codeCompletions);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private Module getModule(String moduleName) {
|
||||
@@ -123,12 +135,13 @@ public class AutoCompletion {
|
||||
private List<CodeCompletion> findCodeCompletionForGlobal(String propertyPrefill) {
|
||||
if (propertyPrefill == null)
|
||||
return Collections.emptyList();
|
||||
List<DictionaryTree.Entry<Property>> result = mGlobalPropertyTree.searchByPrefill(propertyPrefill);
|
||||
List<CodeCompletion> completions = new ArrayList<>();
|
||||
List<DictionaryTree.Entry<Property>> result = mGlobalPropertyTree.searchByPrefill(propertyPrefill);
|
||||
for (DictionaryTree.Entry<Property> entry : result) {
|
||||
Property property = entry.tag;
|
||||
completions.add(new CodeCompletion(property.getKey(), property.getUrl(), propertyPrefill.length()));
|
||||
}
|
||||
mAnyWordsCompletion.findCodeCompletion(completions, propertyPrefill);
|
||||
return completions;
|
||||
}
|
||||
|
||||
|
||||
@@ -246,7 +246,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
mSymbolBar.setCodeCompletions(Symbols.getSymbols());
|
||||
mCodeCompletionBar.setOnHintClickListener(this);
|
||||
mSymbolBar.setOnHintClickListener(this);
|
||||
mAutoCompletion = new AutoCompletion(getContext());
|
||||
mAutoCompletion = new AutoCompletion(getContext(), mEditor.getCodeEditText());
|
||||
mAutoCompletion.setAutoCompleteCallback(mCodeCompletionBar::setCodeCompletions);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.stardust.scriptdroid.ui.edit.completion;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
@@ -74,6 +75,10 @@ public class CodeCompletionBar extends RecyclerView {
|
||||
}
|
||||
|
||||
public void setCodeCompletions(CodeCompletions codeCompletions) {
|
||||
if (Looper.getMainLooper() != Looper.myLooper()) {
|
||||
post(() -> setCodeCompletions(codeCompletions));
|
||||
return;
|
||||
}
|
||||
mCodeCompletions = codeCompletions;
|
||||
getAdapter().notifyDataSetChanged();
|
||||
}
|
||||
@@ -125,5 +130,4 @@ public class CodeCompletionBar extends RecyclerView {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -309,12 +309,12 @@ public class CodeEditText extends AppCompatEditText {
|
||||
}
|
||||
|
||||
private void callCursorChangeCallback(CharSequence text, int sel) {
|
||||
if (getText().length() == 0) {
|
||||
if (text.length() == 0) {
|
||||
return;
|
||||
}
|
||||
if (mCallback == null)
|
||||
return;
|
||||
int lineStart = TextUtils.lastIndexOf(text, '\n', sel) + 1;
|
||||
int lineStart = TextUtils.lastIndexOf(text, '\n', sel - 1) + 1;
|
||||
if (lineStart < 0) {
|
||||
lineStart = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user