-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates a code action for both MarkupEntityMismatch and ETagRequired errors Fixes #588 Signed-off-by: Nikolas Komonen <nikolaskomonen@gmail.com>
- Loading branch information
1 parent
bc9f68b
commit e7aa564
Showing
5 changed files
with
134 additions
and
7 deletions.
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
25 changes: 25 additions & 0 deletions
25
...ipse/lsp4xml/extensions/contentmodel/participants/codeactions/ETagRequiredCodeAction.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,25 @@ | ||
package org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.IComponentProvider; | ||
import org.eclipse.lsp4xml.settings.XMLFormattingOptions; | ||
|
||
/** | ||
* ETagRequiredCodeAction | ||
*/ | ||
public class ETagRequiredCodeAction implements ICodeActionParticipant { | ||
|
||
@Override | ||
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
XMLFormattingOptions formattingSettings, IComponentProvider componentProvider) { | ||
MarkupEntityMismatchCodeAction.createEndTagInsertCodeAction(diagnostic, range, document, codeActions, formattingSettings, componentProvider); | ||
} | ||
|
||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...4xml/extensions/contentmodel/participants/codeactions/MarkupEntityMismatchCodeAction.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,82 @@ | ||
/******************************************************************************* | ||
* 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/el-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions; | ||
|
||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4xml.commons.BadLocationException; | ||
import org.eclipse.lsp4xml.commons.CodeActionFactory; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.dom.DOMElement; | ||
import org.eclipse.lsp4xml.dom.DOMNode; | ||
import org.eclipse.lsp4xml.extensions.contentmodel.participants.XMLSchemaErrorCode; | ||
import org.eclipse.lsp4xml.extensions.contentmodel.participants.XMLSyntaxErrorCode; | ||
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.IComponentProvider; | ||
import org.eclipse.lsp4xml.settings.XMLFormattingOptions; | ||
|
||
/** | ||
* MarkupEntityMismatchCodeAction is a code action that triggers when the end tag of the | ||
* root element is missing. This will provide a codeaction that inserts that missing | ||
* end tag. | ||
*/ | ||
public class MarkupEntityMismatchCodeAction implements ICodeActionParticipant { | ||
private static final Logger LOGGER = Logger.getLogger(MarkupEntityMismatchCodeAction.class.getName()); | ||
|
||
|
||
@Override | ||
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
XMLFormattingOptions formattingSettings, IComponentProvider componentProvider) { | ||
createEndTagInsertCodeAction(diagnostic, range, document, codeActions, formattingSettings, componentProvider); | ||
} | ||
|
||
public static void createEndTagInsertCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
XMLFormattingOptions formattingSettings, IComponentProvider componentProvider) { | ||
try { | ||
int offset = document.offsetAt(diagnostic.getRange().getStart()); | ||
DOMNode node = document.findNodeAt(offset); | ||
if(!node.isElement()) { | ||
return; | ||
} | ||
|
||
DOMElement element = (DOMElement) node; | ||
int startOffset = element.getStartTagOpenOffset(); | ||
Position startPosition = document.positionAt(startOffset); | ||
Position endPosition; | ||
if(XMLSyntaxErrorCode.MarkupEntityMismatch.toString().equals(diagnostic.getCode())) { | ||
endPosition = document.positionAt(document.getEnd()); | ||
if(endPosition.getLine() > startPosition.getLine()) { | ||
endPosition.setCharacter(startPosition.getCharacter()); | ||
} | ||
} | ||
else if(XMLSyntaxErrorCode.ETagRequired.toString().equals(diagnostic.getCode())) { | ||
endPosition = document.positionAt(element.getStartTagCloseOffset() + 1); | ||
} | ||
else { | ||
return; | ||
} | ||
|
||
String elementName = element.getTagName(); | ||
CodeAction action = CodeActionFactory.insert("Close with '</" + elementName + ">'", endPosition, "</" + elementName + ">", document.getTextDocument(), diagnostic); | ||
codeActions.add(action); | ||
} catch (BadLocationException e) { | ||
LOGGER.log(Level.WARNING, "Exception while resolving the code action for " + diagnostic.getCode() + ":", e); | ||
} | ||
} | ||
|
||
|
||
} |
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