-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds editing of start/end tag simultaneously #597
Merged
fbricon
merged 1 commit into
eclipse-lemminx:master
from
NikolasKomonen:matchingTagServer
Dec 13, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 268 additions & 0 deletions
268
org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/XMLPositionUtilityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.lsp4xml.utils; | ||
|
||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4xml.commons.BadLocationException; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.dom.DOMParser; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* XMLPositionUtilityTest | ||
*/ | ||
public class XMLPositionUtilityTest { | ||
|
||
@Test | ||
public void testGetMatchingEndTagPositionMiddle() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Or|ange></Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange></Or|ange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagPositionBeginning() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <|Orange></Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange></|Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagPositionEnd() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange|></Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange></Orange|>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagPositionAttributes() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange| amount=\"1\"></Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange amount=\"1\"></Orange|>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagNoResult() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" |<Orange></Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange></Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagNoResult2() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange |></Orange>\n" + // Because there is a space | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange ></Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagTextBetween() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange|>Text Between</Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange>Text Between</Orange|>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagElementBetween() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange|><Lemon></Lemon></Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange><Lemon></Lemon></Orange|>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingStartTagPositionMiddle() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange></Or|ange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Or|ange></Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingStartTagPositionBeginning() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange></|Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <|Orange></Orange>\n" + | ||
"</Apple>"; | ||
|
||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingStartTagPositionEnd() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange></Orange|>\n" + | ||
"</Apple>"; | ||
|
||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange|></Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingStartTagPositionAttributes() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <Orange amount=\"1\"></Orange|>\n" + | ||
"</Apple>"; | ||
|
||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <Orange| amount=\"1\"></Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagPositionAttributesPrefixed() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <prefix:Orange| amount=\"1\"></prefix:Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <prefix:Orange amount=\"1\"></prefix:Orange|>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
@Test | ||
public void testGetMatchingEndTagPositionPrefixed() { | ||
String initialText= | ||
"<Apple>\n" + | ||
" <pref|ix:Orange></prefix:Orange>\n" + | ||
"</Apple>"; | ||
|
||
String expectedText= | ||
"<Apple>\n" + | ||
" <prefix:Orange></pref|ix:Orange>\n" + | ||
"</Apple>"; | ||
|
||
testMatchingTagPosition(initialText, expectedText); | ||
} | ||
|
||
private static void testMatchingTagPosition(String initialCursorText, String expectedCursorText) { | ||
|
||
int offset = initialCursorText.indexOf('|'); | ||
initialCursorText = initialCursorText.substring(0, offset) + initialCursorText.substring(offset + 1); | ||
DOMDocument xmlDocument = DOMParser.getInstance().parse(initialCursorText, "testURI", null); | ||
Position initialCursorPosition; | ||
Position newCursorPosition; | ||
int newCursorOffset = -1; | ||
try { | ||
initialCursorPosition = xmlDocument.positionAt(offset); | ||
newCursorPosition = XMLPositionUtility.getMatchingTagPosition(xmlDocument, initialCursorPosition); | ||
if(newCursorPosition != null) { // a result for a matching position was found | ||
newCursorOffset = xmlDocument.offsetAt(newCursorPosition); | ||
} | ||
} catch (BadLocationException e) { | ||
Assert.fail(); | ||
return; | ||
} | ||
|
||
StringBuffer sBuffer = new StringBuffer(initialCursorText); | ||
String actualOutputString; | ||
if(newCursorOffset > -1) { | ||
actualOutputString = sBuffer.insert(newCursorOffset, "|").toString(); | ||
} | ||
else { // no matching position was found | ||
actualOutputString = initialCursorText; | ||
} | ||
|
||
Assert.assertEquals(expectedCursorText, actualOutputString); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use StringBuilder