(30);
-
- actions.add(new GsfDefaultKeyTypedAction());
- actions.add(new GsfInsertBreakAction());
- actions.add(new GsfDeleteCharAction(deletePrevCharAction, false));
// The php needs to handle special cases of toggle comment. There has to be
// registered ToggleBlockCommentAction in PHP that handles these special cases,
@@ -226,172 +226,11 @@
/**
* Returns true if bracket completion is enabled in options.
*/
- private static boolean completionSettingEnabled() {
+ static boolean completionSettingEnabled() {
//return ((Boolean)Settings.getValue(GsfEditorKit.class, JavaSettingsNames.PAIR_CHARACTERS_COMPLETION)).booleanValue();
return true;
}
-
- private final class GsfDefaultKeyTypedAction extends ExtDefaultKeyTypedAction {
- private JTextComponent currentTarget;
- private String replacedText = null;
-
- @Override
- public void actionPerformed(ActionEvent evt, JTextComponent target) {
- currentTarget = target;
- super.actionPerformed(evt, target);
- currentTarget = null;
- }
-
- @Override
- protected void insertString(BaseDocument doc, int dotPos, Caret caret, String str,
- boolean overwrite) throws BadLocationException {
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
-
- if (bracketCompletion != null) {
- // TODO - check if we're in a comment etc. and if so, do nothing
- boolean handled =
- bracketCompletion.beforeCharInserted(doc, dotPos, currentTarget,
- str.charAt(0));
-
- if (!handled) {
- super.insertString(doc, dotPos, caret, str, overwrite);
- handled = bracketCompletion.afterCharInserted(doc, dotPos, currentTarget,
- str.charAt(0));
- }
-
- return;
- }
- }
-
- super.insertString(doc, dotPos, caret, str, overwrite);
- }
-
- @Override
- protected void replaceSelection(JTextComponent target, int dotPos, Caret caret,
- String str, boolean overwrite) throws BadLocationException {
- if (str.equals("")) {
- return;
- }
- char insertedChar = str.charAt(0);
- Document document = target.getDocument();
-
- if (document instanceof BaseDocument) {
- BaseDocument doc = (BaseDocument)document;
-
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
-
- if (bracketCompletion != null) {
- try {
- int caretPosition = caret.getDot();
-
- boolean handled =
- bracketCompletion.beforeCharInserted(doc, caretPosition,
- target, insertedChar);
-
- int p0 = Math.min(caret.getDot(), caret.getMark());
- int p1 = Math.max(caret.getDot(), caret.getMark());
-
- if (p0 != p1) {
- doc.remove(p0, p1 - p0);
- }
-
- if (!handled) {
- if ((str != null) && (str.length() > 0)) {
- doc.insertString(p0, str, null);
- }
-
- bracketCompletion.afterCharInserted(doc, caret.getDot() - 1,
- target, insertedChar);
- }
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
-
- return;
- }
- }
- }
-
- super.replaceSelection(target, dotPos, caret, str, overwrite);
- }
- }
-
- private final class GsfInsertBreakAction extends InsertBreakAction {
- static final long serialVersionUID = -1506173310438326380L;
-
- @Override
- protected Object beforeBreak(JTextComponent target, BaseDocument doc, Caret caret) {
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, caret.getDot());
-
- if (bracketCompletion != null) {
- try {
- int newOffset = bracketCompletion.beforeBreak(doc, caret.getDot(), target);
-
- if (newOffset >= 0) {
- return new Integer(newOffset);
- }
- } catch (BadLocationException ble) {
- Exceptions.printStackTrace(ble);
- }
- }
- }
-
- // return Boolean.TRUE;
- return null;
- }
-
- @Override
- protected void afterBreak(JTextComponent target, BaseDocument doc, Caret caret,
- Object cookie) {
- if (completionSettingEnabled()) {
- if (cookie != null) {
- if (cookie instanceof Integer) {
- // integer
- int dotPos = ((Integer)cookie).intValue();
- if (dotPos != -1) {
- caret.setDot(dotPos);
- } else {
- int nowDotPos = caret.getDot();
- caret.setDot(nowDotPos + 1);
- }
- }
- }
- }
- }
- }
-
- private final class GsfDeleteCharAction extends ExtDeleteCharAction {
- private JTextComponent currentTarget;
-
- public GsfDeleteCharAction(String nm, boolean nextChar) {
- super(nm, nextChar);
- }
-
- @Override
- public void actionPerformed(ActionEvent evt, JTextComponent target) {
- currentTarget = target;
- super.actionPerformed(evt, target);
- currentTarget = null;
- }
-
- @Override
- protected void charBackspaced(BaseDocument doc, int dotPos, Caret caret, char ch)
- throws BadLocationException {
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
-
- if (bracketCompletion != null) {
- boolean success = bracketCompletion.charBackspaced(doc, dotPos, currentTarget, ch);
- return;
- }
- }
- super.charBackspaced(doc, dotPos, caret, ch);
- }
- }
-
+
private final class GenericGenerateGoToPopupAction extends NbGenerateGoToPopupAction {
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
@@ -501,6 +340,4 @@
return jm;
}
}
-
-
}
diff --git a/csl.api/src/org/netbeans/modules/csl/core/CslTypedBreakInterceptor.java b/csl.api/src/org/netbeans/modules/csl/core/CslTypedBreakInterceptor.java
new file mode 100644
--- /dev/null
+++ b/csl.api/src/org/netbeans/modules/csl/core/CslTypedBreakInterceptor.java
@@ -0,0 +1,97 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License. When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2012 Sun Microsystems, Inc.
+ */
+package org.netbeans.modules.csl.core;
+
+import javax.swing.text.BadLocationException;
+import org.netbeans.api.editor.mimelookup.MimePath;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.api.editor.mimelookup.MimeRegistrations;
+import org.netbeans.modules.csl.api.KeystrokeHandler;
+import org.netbeans.modules.csl.api.UiUtils;
+import org.netbeans.spi.editor.typinghooks.TypedBreakInterceptor;
+
+public class CslTypedBreakInterceptor implements TypedBreakInterceptor {
+
+ private int caretPosition = -1;
+
+ @Override
+ public boolean beforeInsert(Context context) throws BadLocationException {
+ return false;
+ }
+
+ @Override
+ public void insert(MutableContext context) throws BadLocationException {
+ if (CslEditorKit.completionSettingEnabled()) {
+ KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(context.getDocument(), context.getCaretOffset());
+ if (bracketCompletion != null) {
+ int newOffset = bracketCompletion.beforeBreak(context.getDocument(), context.getCaretOffset(), context.getComponent());
+ if (newOffset >= 0) {
+ caretPosition = newOffset;
+ }
+ }
+ }
+ }
+
+ @Override
+ public void afterInsert(Context context) throws BadLocationException {
+ if (CslEditorKit.completionSettingEnabled()) {
+ if (caretPosition != -1) {
+ context.getComponent().getCaret().setDot(caretPosition);
+ caretPosition = -1;
+ }
+ }
+ }
+
+ @Override
+ public void cancelled(Context context) {
+ }
+
+ @MimeRegistrations(value = {
+ @MimeRegistration(mimeType = "", service = TypedBreakInterceptor.Factory.class)})
+ public static class Factory implements TypedBreakInterceptor.Factory {
+
+ @Override
+ public TypedBreakInterceptor createTypedBreakInterceptor(MimePath mimePath) {
+ return new CslTypedBreakInterceptor();
+ }
+ }
+}
diff --git a/csl.api/src/org/netbeans/modules/csl/core/CslTypedTextInterceptor.java b/csl.api/src/org/netbeans/modules/csl/core/CslTypedTextInterceptor.java
new file mode 100644
--- /dev/null
+++ b/csl.api/src/org/netbeans/modules/csl/core/CslTypedTextInterceptor.java
@@ -0,0 +1,116 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License. When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2012 Sun Microsystems, Inc.
+ */
+package org.netbeans.modules.csl.core;
+
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import org.netbeans.api.editor.mimelookup.MimePath;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.api.editor.mimelookup.MimeRegistrations;
+import org.netbeans.modules.csl.api.KeystrokeHandler;
+import org.netbeans.modules.csl.api.UiUtils;
+import org.netbeans.spi.editor.typinghooks.TypedTextInterceptor;
+
+public class CslTypedTextInterceptor implements TypedTextInterceptor {
+
+ private boolean handled = true;
+
+ @Override
+ public boolean beforeInsert(Context context) throws BadLocationException {
+ return false;
+ }
+
+ @Override
+ public void insert(MutableContext context) throws BadLocationException {
+ if (CslEditorKit.completionSettingEnabled()) {
+ Document doc = context.getDocument();
+ int dotPos = context.getOffset();
+ KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
+ if (bracketCompletion != null) {
+ JTextComponent currentTarget = context.getComponent();
+ String str = context.getText();
+ // TODO - check if we're in a comment etc. and if so, do nothing
+ handled = bracketCompletion.beforeCharInserted(doc, dotPos, currentTarget, str.charAt(0));
+ if (handled) {
+ context.setText("", 0);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void afterInsert(Context context) throws BadLocationException {
+ if (CslEditorKit.completionSettingEnabled()) {
+ if (!handled) {
+ Document doc = context.getDocument();
+ int dotPos = context.getOffset();
+ KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
+ if (bracketCompletion != null) {
+ JTextComponent currentTarget = context.getComponent();
+ String str = context.getText();
+ bracketCompletion.afterCharInserted(doc, dotPos, currentTarget, str.charAt(0));
+ }
+ handled = true;
+ } else {
+ KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(context.getDocument(), context.getOffset());
+ if (bracketCompletion != null) {
+ context.getComponent().getCaret().setDot(context.getComponent().getCaret().getDot() + 1);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void cancelled(Context context) {
+ }
+
+ @MimeRegistrations(value = {
+ @MimeRegistration(mimeType = "", service = TypedTextInterceptor.Factory.class)})
+ public static class Factory implements TypedTextInterceptor.Factory {
+
+ @Override
+ public TypedTextInterceptor createTypedTextInterceptor(MimePath mimePath) {
+ return new CslTypedTextInterceptor();
+ }
+ }
+}
diff --git a/csl.api/src/org/netbeans/modules/csl/core/Language.java b/csl.api/src/org/netbeans/modules/csl/core/Language.java
--- a/csl.api/src/org/netbeans/modules/csl/core/Language.java
+++ b/csl.api/src/org/netbeans/modules/csl/core/Language.java
@@ -252,7 +252,7 @@
} else if (language instanceof DefaultLanguageConfig) {
languageConfig = (DefaultLanguageConfig)language;
}
- }
+ }
return language;
}
@@ -782,5 +782,4 @@
void setOverridingMethodsFile(FileObject fo) {
this.overridingMethodsFile = fo;
}
-
}
diff --git a/csl.api/test/unit/src/org/netbeans/modules/csl/editor/TypingCompletion.java b/csl.api/test/unit/src/org/netbeans/modules/csl/editor/TypingCompletion.java
new file mode 100644
--- /dev/null
+++ b/csl.api/test/unit/src/org/netbeans/modules/csl/editor/TypingCompletion.java
@@ -0,0 +1,199 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License. When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2012 Sun Microsystems, Inc.
+ */
+package org.netbeans.modules.csl.editor;
+
+import java.awt.EventQueue;
+import java.awt.event.KeyEvent;
+import javax.swing.JEditorPane;
+import javax.swing.SwingUtilities;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.EditorKit;
+import junit.framework.TestCase;
+import org.netbeans.api.lexer.Language;
+import org.netbeans.lib.editor.util.CharSequenceUtilities;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.MIMEResolver;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Test brackets completion - unlike the original test this one
+ * emulates real typing and tests the resulting state of the document.
+ */
+public class TypingCompletion {
+ public static final class Context {
+
+ private JEditorPane pane;
+
+ public Context(final EditorKit kit, final Language language, final String mimetype, final String textWithPipe) {
+ try {
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ pane = new JEditorPane();
+ pane.setEditorKit(kit);
+ Document doc = pane.getDocument();
+ // Required by Java's default key typed
+ doc.putProperty(Language.class, language);
+ doc.putProperty("mimeType", mimetype);
+ int caretOffset = textWithPipe.indexOf('|');
+ String text;
+ if (caretOffset != -1) {
+ text = textWithPipe.substring(0, caretOffset) + textWithPipe.substring(caretOffset + 1);
+ } else {
+ text = textWithPipe;
+ }
+ pane.setText(text);
+ pane.setCaretPosition((caretOffset != -1) ? caretOffset : doc.getLength());
+ }
+ });
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ public JEditorPane pane() {
+ return pane;
+ }
+
+ public Document document() {
+ return pane.getDocument();
+ }
+
+ public void typeChar(final char ch) {
+ try {
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ KeyEvent keyEvent;
+ switch (ch) {
+ case '\n':
+ keyEvent = new KeyEvent(pane, KeyEvent.KEY_PRESSED,
+ EventQueue.getMostRecentEventTime(),
+ 0, KeyEvent.VK_ENTER, KeyEvent.CHAR_UNDEFINED); // Simulate pressing of Enter
+ break;
+ case '\b':
+ keyEvent = new KeyEvent(pane, KeyEvent.KEY_PRESSED,
+ EventQueue.getMostRecentEventTime(),
+ 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED); // Simulate pressing of BackSpace
+ break;
+ case '\f':
+ keyEvent = new KeyEvent(pane, KeyEvent.KEY_PRESSED,
+ EventQueue.getMostRecentEventTime(),
+ 0, KeyEvent.VK_DELETE, KeyEvent.CHAR_UNDEFINED); // Simulate pressing of Delete
+ break;
+ default:
+ keyEvent = new KeyEvent(pane, KeyEvent.KEY_TYPED,
+ EventQueue.getMostRecentEventTime(),
+ 0, KeyEvent.VK_UNDEFINED, ch);
+ }
+ SwingUtilities.processKeyBindings(keyEvent);
+ }
+ });
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ public void typeText(String text) {
+ for (int i = 0; i < text.length(); i++) {
+ typeChar(text.charAt(i));
+ }
+ }
+
+ public void assertDocumentTextEquals(final String textWithPipe) {
+ try {
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ int caretOffset = textWithPipe.indexOf('|');
+ String text;
+ if (caretOffset != -1) {
+ text = textWithPipe.substring(0, caretOffset) + textWithPipe.substring(caretOffset + 1);
+ } else {
+ text = textWithPipe;
+ }
+ try {
+ // Use debug text to prefix special chars for easier readability
+ text = CharSequenceUtilities.debugText(text);
+ String docText = document().getText(0, document().getLength());
+ docText = CharSequenceUtilities.debugText(docText);
+ if (!text.equals(docText)) {
+ int diffIndex = 0;
+ int minLen = Math.min(docText.length(), text.length());
+ while (diffIndex < minLen) {
+ if (text.charAt(diffIndex) != docText.charAt(diffIndex)) {
+ break;
+ }
+ diffIndex++;
+ }
+ TestCase.fail("Invalid document text - diff at index " + diffIndex +
+ "\nExpected: \"" + text +
+ "\"\n Actual: \"" + docText + "\""
+ );
+ }
+ } catch (BadLocationException e) {
+ throw new IllegalStateException(e);
+ }
+ if (caretOffset != -1) {
+ TestCase.assertEquals("Invalid caret offset", caretOffset, pane.getCaretPosition());
+ }
+ }
+ });
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ }
+
+ @ServiceProvider(service=MIMEResolver.class)
+ public static final class MIMEResolverImpl extends MIMEResolver {
+
+ public MIMEResolverImpl() {
+ super("text/x-nbeditor-keybindingsettings");
+ }
+
+ @Override public String findMIMEType(FileObject fo) {
+ return fo.getPath().contains("Keybindings") ? "text/x-nbeditor-keybindingsettings" : null;
+ }
+ }
+}
diff --git a/html.editor/src/org/netbeans/modules/html/editor/api/HtmlKit.java b/html.editor/src/org/netbeans/modules/html/editor/api/HtmlKit.java
--- a/html.editor/src/org/netbeans/modules/html/editor/api/HtmlKit.java
+++ b/html.editor/src/org/netbeans/modules/html/editor/api/HtmlKit.java
@@ -123,25 +123,10 @@
c.setTransferHandler(new HtmlTransferHandler());
}
- protected DeleteCharAction createDeletePrevAction() {
- return new HtmlDeleteCharAction(deletePrevCharAction, false);
- }
-
- protected ExtDefaultKeyTypedAction createDefaultKeyTypedAction() {
- return new HtmlDefaultKeyTypedAction();
- }
-
- protected InsertBreakAction createInsertBreakAction() {
- return new HtmlInsertBreakAction();
- }
@Override
protected Action[] createActions() {
Action[] HtmlActions = new Action[]{
- createInsertBreakAction(),
- createDefaultKeyTypedAction(),
- createDeletePrevAction(),
- new HtmlDeleteCharAction(deleteNextCharAction, true),
CslActions.createSelectCodeElementAction(true),
CslActions.createSelectCodeElementAction(false),
CslActions.createInstantRenameAction(),
@@ -370,190 +355,6 @@
}
}
-
- public class HtmlInsertBreakAction extends InsertBreakAction {
-
- static final long serialVersionUID = -1506173310438326380L;
-
- @Override
- protected Object beforeBreak(JTextComponent target, BaseDocument doc, Caret caret) {
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, caret.getDot());
-
- if (bracketCompletion != null) {
- try {
- int newOffset = bracketCompletion.beforeBreak(doc, caret.getDot(), target);
-
- if (newOffset >= 0) {
- return new Integer(newOffset);
- }
- } catch (BadLocationException ble) {
- Exceptions.printStackTrace(ble);
- }
- }
- }
-
- // return Boolean.TRUE;
- return null;
- }
-
- @Override
- protected void afterBreak(JTextComponent target, BaseDocument doc, Caret caret,
- Object cookie) {
- if (completionSettingEnabled()) {
- if (cookie != null) {
- if (cookie instanceof Integer) {
- // integer
- int dotPos = ((Integer) cookie).intValue();
- if (dotPos != -1) {
- caret.setDot(dotPos);
- } else {
- int nowDotPos = caret.getDot();
- caret.setDot(nowDotPos + 1);
- }
- }
- }
- }
- }
- }
-
- public static class HtmlDefaultKeyTypedAction extends ExtDefaultKeyTypedAction {
-
- private JTextComponent currentTarget;
-
- @Override
- public void actionPerformed(ActionEvent evt, JTextComponent target) {
- currentTarget = target;
- super.actionPerformed(evt, target);
- currentTarget = null;
- }
-
- @Override
- protected void insertString(BaseDocument doc, int dotPos,
- Caret caret, String str,
- boolean overwrite) throws BadLocationException {
-
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
-
- if (bracketCompletion != null) {
- // TODO - check if we're in a comment etc. and if so, do nothing
- boolean handled =
- bracketCompletion.beforeCharInserted(doc, dotPos, currentTarget,
- str.charAt(0));
-
- if (!handled) {
- super.insertString(doc, dotPos, caret, str, overwrite);
- handled = bracketCompletion.afterCharInserted(doc, dotPos, currentTarget,
- str.charAt(0));
- }
-
-
- return;
- }
- }
-
- super.insertString(doc, dotPos, caret, str, overwrite);
- }
-
- @Override
- protected void replaceSelection(JTextComponent target, int dotPos, Caret caret,
- String str, boolean overwrite) throws BadLocationException {
-
- //workaround for #209019 - regression of issue
- //#204450 - Rewrite actions to use TypingHooks SPI
- if(str.length() == 0) {
- //called from BaseKit.actionPerformed():1160 with empty str argument
- //==> ignore this call since we are going to be called a bit later
- //from HtmlKit.performTextInsertion() properly with the text typed
- return ;
- }
-
- char insertedChar = str.charAt(0);
- Document document = target.getDocument();
-
- if (document instanceof BaseDocument) {
- BaseDocument doc = (BaseDocument) document;
-
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
-
- if (bracketCompletion != null) {
- try {
- int caretPosition = caret.getDot();
-
- boolean handled =
- bracketCompletion.beforeCharInserted(doc, caretPosition,
- target, insertedChar);
-
- int p0 = Math.min(caret.getDot(), caret.getMark());
- int p1 = Math.max(caret.getDot(), caret.getMark());
-
- if (p0 != p1) {
- doc.remove(p0, p1 - p0);
- }
-
- if (!handled) {
- if ((str != null) && (str.length() > 0)) {
- doc.insertString(p0, str, null);
- }
-
- bracketCompletion.afterCharInserted(doc, caret.getDot() - 1,
- target, insertedChar);
-
- }
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
-
- return;
- }
- }
- }
-
- super.replaceSelection(target, dotPos, caret, str, overwrite);
- }
-
- }
-
- public static class HtmlDeleteCharAction extends DeleteCharAction {
-
- private JTextComponent currentTarget;
-
- public HtmlDeleteCharAction(String name, boolean nextChar) {
- super(name, nextChar);
- }
-
- @Override
- public void actionPerformed(ActionEvent evt, JTextComponent target) {
- try {
- currentTarget = target;
- super.actionPerformed(evt, target);
- } finally {
- currentTarget = null;
- }
- }
-
- @Override
- protected void charBackspaced(BaseDocument doc, int dotPos, Caret caret, char ch) throws BadLocationException {
- if (completionSettingEnabled()) {
- KeystrokeHandler bracketCompletion = UiUtils.getBracketCompletion(doc, dotPos);
-
- if (bracketCompletion != null) {
- boolean success = bracketCompletion.charBackspaced(doc, dotPos, currentTarget, ch);
- return;
- }
- }
-
-
- super.charBackspaced(doc, dotPos, caret, ch);
- }
-
- public boolean getNextChar() {
- return nextChar;
- }
- }
-
/* !!!!!!!!!!!!!!!!!!!!!
*
* Inner classes bellow were taken from BasicTextUI and rewritten in the place marked
diff --git a/java.editor/test/unit/src/org/netbeans/modules/editor/java/TypingCompletionUnitTest.java b/html.editor/test/unit/src/org/netbeans/modules/html/editor/gsf/TypingCompletionUnitTest.java
copy from java.editor/test/unit/src/org/netbeans/modules/editor/java/TypingCompletionUnitTest.java
copy to html.editor/test/unit/src/org/netbeans/modules/html/editor/gsf/TypingCompletionUnitTest.java
--- a/java.editor/test/unit/src/org/netbeans/modules/editor/java/TypingCompletionUnitTest.java
+++ b/html.editor/test/unit/src/org/netbeans/modules/html/editor/gsf/TypingCompletionUnitTest.java
@@ -42,36 +42,21 @@
* made subject to such option by the copyright holder.
*/
-package org.netbeans.modules.editor.java;
+package org.netbeans.modules.html.editor.gsf;
-import java.awt.EventQueue;
-import java.awt.event.KeyEvent;
import java.util.prefs.Preferences;
-import java.util.regex.Pattern;
-import javax.swing.JEditorPane;
-import javax.swing.SwingUtilities;
-import javax.swing.text.BadLocationException;
-import javax.swing.text.Document;
-import javax.swing.text.EditorKit;
-import javax.swing.text.PlainDocument;
-import junit.framework.TestCase;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.settings.SimpleValueNames;
-import org.netbeans.api.java.lexer.JavaTokenId;
-import org.netbeans.api.lexer.Language;
+import org.netbeans.api.html.lexer.HTMLTokenId;
import org.netbeans.junit.NbTestCase;
-import org.netbeans.lib.editor.util.CharSequenceUtilities;
+import org.netbeans.modules.csl.editor.TypingCompletion;
+import org.netbeans.modules.html.editor.api.HtmlKit;
import org.openide.awt.AcceleratorBinding;
-import org.openide.filesystems.FileObject;
-import org.openide.filesystems.MIMEResolver;
-import org.openide.util.lookup.ServiceProvider;
/**
* Test java brackets completion - unlike the original test this one
* emulates real typing and tests the resulting state of the document.
- *
- * @autor Miloslav Metelka
*/
public class TypingCompletionUnitTest extends NbTestCase {
@@ -82,1311 +67,94 @@
@Override
protected void setUp() throws Exception {
super.setUp();
- Preferences prefs = MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE).lookup(Preferences.class);
+ Preferences prefs = MimeLookup.getLookup(HtmlKit.HTML_MIME_TYPE).lookup(Preferences.class);
prefs.putBoolean(SimpleValueNames.COMPLETION_PAIR_CHARACTERS, true);
Class.forName(AcceleratorBinding.class.getName(), true, AcceleratorBinding.class.getClassLoader());
}
// ------- Tests for completion of right parenthesis ')' -------------
- public void testTypeSemicolonInForLoop() { // #146139
- Context ctx = new Context(new JavaKit(),
- "for (int i = 0|)"
+ public void testQuoteCompletion() {
+ TypingCompletion.Context ctx = new TypingCompletion.Context(new HtmlKit(), HTMLTokenId.language(), HtmlKit.HTML_MIME_TYPE,
+ " 6) {\n" +
- "}"
- );
- ctx.typeChar(')');
- ctx.assertDocumentTextEquals(
- "if (a())| + 5 > 6) {\n" +
- "}"
- );
- }
-
- public void testTypeRightParenNoSkipNonBracketChar() {
- Context ctx = new Context(new JavaKit(),
- "m()|"
- );
- ctx.typeChar(' ');
- ctx.assertDocumentTextEquals(
- "m() |"
- );
- }
-
-
-
- // ------- Tests for completion of right brace '}' -------------
-
- public void testTypeAddRightBraceIfLeftBrace() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) {|\n" +
- " }\n" +
- "}\n"
- );
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) {\n" +
- " |\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void testTypeAddRightBraceIfLeftBraceWhiteSpace() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) { \t|\n" +
- " }\n" +
- "}\n"
- );
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) { \t\n" +
- " |\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void testTypeAddRightBraceIfLeftBraceLineComment() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) { // line-comment|\n" +
- " }\n" +
- "}\n"
- );
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) { // line-comment\n" +
- " |\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void testTypeAddRightBraceIfLeftBraceBlockComment() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) { /* block-comment */|\n" +
- " }\n" +
- "}\n"
- );
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) { /* block-comment */\n" +
- " |\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void testTypeAddRightBraceIfLeftBraceAlreadyPresent() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) {|\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) {\n" +
- " |\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void XtestTypeAddRightBraceCaretInComment() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) { /* unclosed-block-comment|\n" +
- " */\n" +
- " }\n" +
- "}\n"
- );
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) { /* unclosed-block-comment\n" +
- " * |\n" +
- " */\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void testTypeAddRightBraceMultiLine() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) {| System.out.println(\n" +
- " \"\");\n" +
- " }\n" +
- "}\n"
- );
-
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) {\n" +
- " System.out.println(\n" +
- " \"\");\n" +
- " }\n" +
- "}\n"
- );
- }
-
- public void testTypeAddRightBraceSingleLine() {
- Context ctx = new Context(new JavaKit(),
- "class Test {\n" +
- " {\n" +
- " if (true) {| System.out.println(\"\");\n" +
- " }\n" +
- "}\n"
- );
-
- ctx.typeChar('\n');
- ctx.assertDocumentTextEquals(
- "class Test {\n" +
- " {\n" +
- " if (true) {\n" +
- " System.out.println(\"\");\n" +
- " }\n" +
- " }\n" +
- "}\n"
- );
- }
-
-
- // ------- Tests for completion of quote (") -------------
- public void testTypeSimpleQuoteInEmptyDoc () throws Exception {
- Context ctx = new Context(new JavaKit(),
- "|"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "\"|\""
- );
- }
-
- public void testTypeSimpleQuoteAtBeginingOfDoc () throws Exception {
- Context ctx = new Context(new JavaKit(),
- "| "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "\"|\" "
- );
- }
-
- public void testTypeSimpleQuoteAtEndOfDoc () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " |"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " \"|\""
- );
- }
-
- public void testTypeSimpleQuoteInWhiteSpaceArea () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " | "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " \"|\" "
- );
- }
-
- public void testTypeQuoteAtEOL () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " |\n"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " \"|\"\n"
- );
- }
-
- public void testTypeQuoteWithUnterminatedStringLiteral () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " \"unterminated string| \n"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " \"unterminated string\"| \n"
- );
- }
-
- public void testTypeQuoteAtEOLWithUnterminatedStringLiteral () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " \"unterminated string | \n"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " \"unterminated string \"| \n"
- );
- }
-
- public void testTypeQuoteInsideStringLiteral () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " \"stri|ng literal\" "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " \"stri\"|ng literal\" "
- );
- }
-
- public void testTypeQuoteInsideEmptyParentheses () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " System.out.println(|) "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " System.out.println(\"|\") "
- );
- }
-
- public void testTypeQuoteInsideNonEmptyParentheses () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " System.out.println(|some text) "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " System.out.println(\"|some text) "
- );
- }
-
- public void testTypeQuoteInsideNonEmptyParenthesesBeforeClosingParentheses () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " System.out.println(i+|) "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " System.out.println(i+\"|\") "
- );
- }
-
- public void testTypeQuoteInsideNonEmptyParenthesesBeforeClosingParenthesesAndUnterminatedStringLiteral () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " System.out.println(\"unterminated string literal |); "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " System.out.println(\"unterminated string literal \"|); "
- );
- }
-
- public void testTypeQuoteBeforePlus () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " System.out.println(|+\"string literal\"); "
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- " System.out.println(\"|\"+\"string literal\"); "
- );
- }
-
- public void testTypeQuoteBeforeComma () throws Exception {
- Context ctx = new Context(new JavaKit(),
- "String s[] = new String[]{|,\"two\"};"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "String s[] = new String[]{\"|\",\"two\"};"
- );
- }
-
- public void testTypeQuoteBeforeBrace () throws Exception {
- Context ctx = new Context(new JavaKit(),
- "String s[] = new String[]{\"one\",|};"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "String s[] = new String[]{\"one\",\"|\"};"
- );
- }
-
- public void testTypeQuoteBeforeSemicolon() throws Exception {
- Context ctx = new Context(new JavaKit(),
- "String s = \"\" + |;"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "String s = \"\" + \"|\";"
- );
- }
-
- public void testTypeQuoteBeforeSemicolonWithWhitespace() throws Exception {
- Context ctx = new Context(new JavaKit(),
- "String s = \"\" +| ;"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "String s = \"\" +\"|\" ;"
- );
- }
-
- public void testTypeQuoteAfterEscapeSequence() throws Exception {
- Context ctx = new Context(new JavaKit(),
- "\\|"
- );
- ctx.typeChar('"');
- ctx.assertDocumentTextEquals(
- "\\\"|"
- );
- }
-
- public void testTypeQuoteEaten() throws Exception {
- Context ctx = new Context(new JavaKit(),
- "|"
- );
- ctx.typeChar('"');
ctx.typeChar('"');
ctx.assertDocumentTextEquals(
"\"\"|"
);
}
-
- public void testTypeQuoteOnFirstQuote () throws Exception {
- Context ctx = new Context(new JavaKit(),
- " |\"asdf\""
+
+ public void testBackSlash() {
+ TypingCompletion.Context ctx = new TypingCompletion.Context(new HtmlKit(), HTMLTokenId.language(), HtmlKit.HTML_MIME_TYPE,
+ "
|"
+ );
+ }
+
+ public void testChangeQuote() {
+ TypingCompletion.Context ctx = new TypingCompletion.Context(new HtmlKit(), HTMLTokenId.language(), HtmlKit.HTML_MIME_TYPE,
+ "