You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/08/07 16:35:46 UTC

[GitHub] matthiasblaesing closed pull request #642: [NETBEANS-1083] Fix value replacement in POM editing

matthiasblaesing closed pull request #642: [NETBEANS-1083] Fix value replacement in POM editing
URL: https://github.com/apache/incubator-netbeans/pull/642
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/xml.text/src/org/netbeans/modules/xml/text/completion/XMLResultItem.java b/xml.text/src/org/netbeans/modules/xml/text/completion/XMLResultItem.java
index c482b19a4f..297a4bf343 100644
--- a/xml.text/src/org/netbeans/modules/xml/text/completion/XMLResultItem.java
+++ b/xml.text/src/org/netbeans/modules/xml/text/completion/XMLResultItem.java
@@ -217,11 +217,12 @@ boolean replaceText( JTextComponent component, final String replaceToText, int o
         }
         return true;
     }
-    
+
     private boolean isRemovingAvailableLocked(TokenSequence ts, Document doc, int offset, String replaceToText) {
-        boolean isTextRemovingAllowable = true;
-        
-        while (ts.moveNext() && isTextRemovingAllowable) {
+        ts.move(offset);
+        boolean isTextRemovingAllowable = false;
+
+        if (ts.moveNext()) {
             Token<XMLTokenId> tokenItem = ts.token();
             String tokenItemImage = tokenItem.text().toString();
             if ((tokenItemImage != null) && (tokenItemImage.length() > 0)) {
diff --git a/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/ValueResultItemTest.java b/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/ValueResultItemTest.java
new file mode 100644
index 0000000000..5526068f6f
--- /dev/null
+++ b/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/ValueResultItemTest.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.xml.text.completion;
+
+import javax.swing.Icon;
+import javax.swing.JTextField;
+import javax.swing.text.Document;
+import org.junit.Test;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.editor.Finder;
+import org.netbeans.editor.FinderFactory;
+import org.netbeans.modules.xml.api.model.GrammarResult;
+import org.netbeans.modules.xml.spi.dom.AbstractNode;
+import org.netbeans.modules.xml.text.AbstractTestCase;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+public class ValueResultItemTest extends AbstractTestCase {
+
+    public ValueResultItemTest(String testName) {
+        super(testName);
+    }
+
+    @Test
+    public void testValueReplacement() throws Exception {
+        JTextField textField = new JTextField();
+        BaseDocument doc = getDocument("/org/netbeans/modules/xml/text/completion/res/ValueReplacement.xml");
+        BaseDocument referenceDoc = getDocument("/org/netbeans/modules/xml/text/completion/res/ValueReplacement.golden.xml");
+        Finder ip1Finder = new FinderFactory.StringFwdFinder("##IP1##", true);
+        int insertPos1 = doc.find(ip1Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
+        doc.replace(insertPos1, 7, "", null);
+        textField.setDocument(doc);
+        MockGrammarResult mgr = new MockGrammarResult("Middle", "Mid");
+        ValueResultItem vri1 = new ValueResultItem(insertPos1, mgr, 0, null);
+        textField.setCaretPosition(insertPos1);
+        vri1.defaultAction(textField);
+
+        Finder ip2Finder = new FinderFactory.StringFwdFinder("##IP2##", true);
+        int insertPos2 = doc.find(ip2Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
+        doc.replace(insertPos2, 7, "", null);
+        ValueResultItem vri2 = new ValueResultItem(insertPos2, mgr, 7, null);
+        textField.setCaretPosition(insertPos2);
+        vri2.defaultAction(textField);
+
+        MockGrammarResult mgr2 = new MockGrammarResult("Middle", "");
+        Finder ip3Finder = new FinderFactory.StringFwdFinder("##IP3##", true);
+        int insertPos3 = doc.find(ip3Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
+        doc.replace(insertPos3, 7, "", null);
+        ValueResultItem vri3 = new ValueResultItem(insertPos3, mgr2, 7, null);
+        textField.setCaretPosition(insertPos3);
+        vri3.defaultAction(textField);
+
+        assertTrue("Result did not match reference", compare(referenceDoc, doc));
+    }
+
+    private static class MockGrammarResult extends AbstractNode implements GrammarResult, Text {
+        private final String name;
+        private final String prefix;
+        private final String description = "";
+
+        public MockGrammarResult(String name, String prefix) {
+            this.name = name;
+            this.prefix = prefix;
+        }
+
+        @Override
+        public short getNodeType() {
+            return Node.TEXT_NODE;
+        }
+
+        @Override
+        public String getNodeName() {
+            return name;
+        }
+
+        @Override
+        public String getTagName() {
+            return name;
+        }
+
+        @Override
+        public String getNodeValue() {
+            return name.substring(prefix.length());
+        }
+
+        @Override
+        public String getDescription() {
+            return description;
+        }
+
+        @Override
+        public String getDisplayName() {
+            return name;
+        }
+
+        @Override
+        public Icon getIcon(int kind) {
+            return null;
+        }
+
+        @Override
+        public boolean isEmptyElement() {
+            return false;
+        }
+
+    }
+}
diff --git a/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/res/ValueReplacement.golden.xml b/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/res/ValueReplacement.golden.xml
new file mode 100644
index 0000000000..5af0c5490e
--- /dev/null
+++ b/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/res/ValueReplacement.golden.xml
@@ -0,0 +1,25 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<root>
+    <element1>Middle</element1>
+    <element1>Middle</element1>
+    <element2>MiddleStart</element2>
+</root>
diff --git a/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/res/ValueReplacement.xml b/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/res/ValueReplacement.xml
new file mode 100644
index 0000000000..f5c9adc302
--- /dev/null
+++ b/xml.text/test/unit/src/org/netbeans/modules/xml/text/completion/res/ValueReplacement.xml
@@ -0,0 +1,25 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<root>
+    <element1>Mid##IP1##</element1>
+    <element1>Mid##IP2##Element</element1>
+    <element2>##IP3##Start</element2>
+</root>
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists