Commit 685a469c authored by Alexander Weigl's avatar Alexander Weigl

fixes in completionposition

parent 3e0f3a67
...@@ -15,16 +15,17 @@ public class CompletionPosition { ...@@ -15,16 +15,17 @@ public class CompletionPosition {
int pos; int pos;
public String getPrefix() { public String getPrefix() {
int start; if (text.isEmpty()) return "";
for (start = pos; start >= 0; start--) { int start = Math.min(text.length() - 1, pos - 1);
for (; start >= 0; start--) {
if (Character.isWhitespace(text.charAt(start))) if (Character.isWhitespace(text.charAt(start)))
break; break;
} }
return text.substring(start, pos).trim(); return text.substring(Math.max(0, start), pos).trim();
} }
public boolean onLineBegin() { public boolean onLineBegin() {
for (int i = pos; i >= 0; i--) { for (int i = Math.min(text.length() - 1, pos); i >= 0; i--) {
if (text.charAt(i) == ' ' || text.charAt(i) == '\t') continue; if (text.charAt(i) == ' ' || text.charAt(i) == '\t') continue;
if (text.charAt(i) == '\n') return true; if (text.charAt(i) == '\n') return true;
return false; return false;
......
...@@ -33,10 +33,7 @@ import javafx.geometry.Point2D; ...@@ -33,10 +33,7 @@ import javafx.geometry.Point2D;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.input.ContextMenuEvent; import javafx.scene.input.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint; import javafx.scene.paint.Paint;
...@@ -62,6 +59,9 @@ import org.fxmisc.richtext.*; ...@@ -62,6 +59,9 @@ import org.fxmisc.richtext.*;
import org.fxmisc.richtext.event.MouseOverTextEvent; import org.fxmisc.richtext.event.MouseOverTextEvent;
import org.fxmisc.richtext.model.*; import org.fxmisc.richtext.model.*;
import org.fxmisc.undo.UndoManager; import org.fxmisc.undo.UndoManager;
import org.fxmisc.wellbehaved.event.EventPattern;
import org.fxmisc.wellbehaved.event.InputMap;
import org.fxmisc.wellbehaved.event.Nodes;
import org.reactfx.EventStream; import org.reactfx.EventStream;
import org.reactfx.SuspendableNo; import org.reactfx.SuspendableNo;
import org.reactfx.collection.LiveList; import org.reactfx.collection.LiveList;
...@@ -78,6 +78,11 @@ import java.util.function.IntFunction; ...@@ -78,6 +78,11 @@ import java.util.function.IntFunction;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static javafx.scene.input.KeyCombination.SHORTCUT_DOWN;
import static org.fxmisc.wellbehaved.event.EventPattern.keyPressed;
import static org.fxmisc.wellbehaved.event.InputHandler.Result.PROCEED;
import static org.fxmisc.wellbehaved.event.InputMap.*;
/** /**
* ScriptArea is the {@link CodeArea} for writing Proof Scripts. * ScriptArea is the {@link CodeArea} for writing Proof Scripts.
* <p> * <p>
...@@ -145,25 +150,43 @@ public class ScriptArea extends BorderPane { ...@@ -145,25 +150,43 @@ public class ScriptArea extends BorderPane {
private void init() { private void init() {
codeArea.setAutoScrollOnDragDesired(false); codeArea.setAutoScrollOnDragDesired(false);
codeArea.setOnKeyReleased(event -> { InputMap<KeyEvent> inputMap = sequence(
inlineToolbar.hide(); process(EventPattern.keyPressed(),
if (event.isControlDown() && event.getCode() == KeyCode.ENTER) (e) -> {
simpleReformat(); if (autoCompletion.isVisible()) autoCompletion.update();
if (event.isControlDown() && event.getCode() == KeyCode.H) inlineToolbar.hide();
inlineToolbar.show(); return PROCEED;
if (event.isControlDown() && event.getCode() == KeyCode.SPACE) { }),
autoCompletion.update(); consumeWhen(keyPressed(KeyCode.ENTER), autoCompletion::isVisible,
autoCompletion.show(); e -> autoCompletion.complete()),
} consume(keyPressed(KeyCode.ENTER, SHORTCUT_DOWN),
if (autoCompletion.isVisible()) (e) -> simpleReformat()),
autoCompletion.update(); consume(keyPressed(KeyCode.H, SHORTCUT_DOWN),
}); (e) -> inlineToolbar.show()),
consume(keyPressed(KeyCode.SPACE, SHORTCUT_DOWN), e -> {
if (autoCompletion.isVisible()) {
autoCompletion.hide();
} else {
autoCompletion.update();
autoCompletion.show();
}
}),
consumeWhen(keyPressed(KeyCode.DOWN), autoCompletion::isVisible, e -> autoCompletion.selection(+1)),
consumeWhen(keyPressed(KeyCode.UP), autoCompletion::isVisible, e -> autoCompletion.selection(-1)),
consumeWhen(keyPressed(KeyCode.PAGE_DOWN), autoCompletion::isVisible, e -> autoCompletion.selection(+3)),
consumeWhen(keyPressed(KeyCode.PAGE_UP), autoCompletion::isVisible, e -> autoCompletion.selection(-3)),
consume(keyPressed(KeyCode.ESCAPE), e -> {
autoCompletion.hide();
inlineToolbar.hide();
}));
Nodes.addInputMap(codeArea, inputMap);
setCenter(scrollPane); setCenter(scrollPane);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.widthProperty().addListener((a, b, c) -> { scrollPane.widthProperty().addListener((a, b, c) ->
codeArea.setMinSize(scrollPane.getWidth(), scrollPane.getHeight()); codeArea.setMinSize(scrollPane.getWidth(), scrollPane.getHeight()));
});
/* /*
scrollPane.estimatedScrollYProperty().addListener(new ChangeListener<Double>() { scrollPane.estimatedScrollYProperty().addListener(new ChangeListener<Double>() {
...@@ -551,7 +574,6 @@ public class ScriptArea extends BorderPane { ...@@ -551,7 +574,6 @@ public class ScriptArea extends BorderPane {
public CodePointCharStream getStream() { public CodePointCharStream getStream() {
return CharStreams.fromString(getText(), getFilePath().getAbsolutePath()); return CharStreams.fromString(getText(), getFilePath().getAbsolutePath());
} }
public String getText() { public String getText() {
...@@ -1801,6 +1823,7 @@ public class ScriptArea extends BorderPane { ...@@ -1801,6 +1823,7 @@ public class ScriptArea extends BorderPane {
/*private AutoCompletePopup<Suggestion> popup = new AutoCompletePopup<>(); /*private AutoCompletePopup<Suggestion> popup = new AutoCompletePopup<>();
private ListView<Suggestion> suggestionView; private ListView<Suggestion> suggestionView;
private ObservableList<Suggestion> suggestions;*/ private ObservableList<Suggestion> suggestions;*/
private int lastSelected = -1;
private Stage popup; private Stage popup;
private ListView<Suggestion> suggestionView = new ListView<>(); private ListView<Suggestion> suggestionView = new ListView<>();
private ObservableList<Suggestion> suggestions; private ObservableList<Suggestion> suggestions;
...@@ -1811,7 +1834,11 @@ public class ScriptArea extends BorderPane { ...@@ -1811,7 +1834,11 @@ public class ScriptArea extends BorderPane {
popup.setSkin(new AutoCompletePopupSkin<>(popup)); popup.setSkin(new AutoCompletePopupSkin<>(popup));
suggestionView = (ListView<Suggestion>) popup.getSkin().getNode();*/ suggestionView = (ListView<Suggestion>) popup.getSkin().getNode();*/
suggestions = suggestionView.getItems(); suggestions = suggestionView.getItems();
suggestionView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
suggestionView.getSelectionModel().getSelectedIndices().addListener((InvalidationListener) observable -> {
System.out.println(" = " + suggestionView.getSelectionModel().getSelectedIndex());
//lastSelected = suggestionView.getSelectionModel().getSelectedIndex();
});
//popup.setVisibleRowCount(5); //popup.setVisibleRowCount(5);
suggestionView.setEditable(false); suggestionView.setEditable(false);
...@@ -1844,6 +1871,7 @@ public class ScriptArea extends BorderPane { ...@@ -1844,6 +1871,7 @@ public class ScriptArea extends BorderPane {
//System.out.println("searchPrefix = " + searchPrefix); //System.out.println("searchPrefix = " + searchPrefix);
CompletionPosition cp = new CompletionPosition(getText(), end); CompletionPosition cp = new CompletionPosition(getText(), end);
System.out.println("cp.getPrefix() = " + cp.getPrefix());
List<Suggestion> newS = autoCompletionController.getSuggestions(cp); List<Suggestion> newS = autoCompletionController.getSuggestions(cp);
suggestions.setAll(newS); suggestions.setAll(newS);
...@@ -1857,7 +1885,7 @@ public class ScriptArea extends BorderPane { ...@@ -1857,7 +1885,7 @@ public class ScriptArea extends BorderPane {
public void show() { public void show() {
//popup.show(ScriptArea.this.getScene().getWindow()); //popup.show(ScriptArea.this.getScene().getWindow());
popup.show(); popup.show();
codeArea.requestFocus();
} }
public void hide() { public void hide() {
...@@ -1867,6 +1895,28 @@ public class ScriptArea extends BorderPane { ...@@ -1867,6 +1895,28 @@ public class ScriptArea extends BorderPane {
public boolean isVisible() { public boolean isVisible() {
return popup != null && popup.isShowing(); return popup != null && popup.isShowing();
} }
public void complete() {
String entry = suggestions.get(lastSelected).getText();
codeArea.selectWord();
codeArea.replaceSelection(entry);
hide();
codeArea.requestFocus();
}
public void selection(int relline) {
if (lastSelected < 0) {
if (relline < 0) {
lastSelected = suggestionView.getItems().size() - 1;
} else {
lastSelected = 0;
}
} else {
lastSelected += relline;
}
suggestionView.getSelectionModel().select(lastSelected);
suggestionView.scrollTo(lastSelected);
}
} }
} }
......
package edu.kit.iti.formal.psdbg.gui.actions.acomplete;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Alexander Weigl
* @version 1 (14.03.18)
*/
public class CompletionPositionTest {
private CompletionPosition a, b, c, d, e;
@Before
public void setup() {
a = create("abc| def ghi");
b = create("abc |def ghi");
c = create("abc\ndef\nghi\n|");
d = new CompletionPosition("", 3);
e = create("abc\ndef\n|\nghi\n");
}
private static CompletionPosition create(String s) {
return new CompletionPosition(s.replace("|", ""), s.indexOf('|'));
}
@Test
public void getPrefix() throws Exception {
assertEquals("abc", a.getPrefix());
assertEquals("", b.getPrefix());
assertEquals("", c.getPrefix());
assertEquals("", d.getPrefix());
assertEquals("", e.getPrefix());
}
@Test
public void onLineBegin() throws Exception {
assertFalse(a.onLineBegin());
assertFalse(b.onLineBegin());
assertTrue(c.onLineBegin());
assertTrue(d.onLineBegin());
assertTrue(e.onLineBegin());
}
@Test
public void filterByPrefix() throws Exception {
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment