-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error range for src-import.1.2, add code actions
Signed-off-by: David Kwon <[email protected]>
- Loading branch information
1 parent
05a522d
commit b153a54
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...se/lsp4xml/extensions/contentmodel/participants/codeactions/src_import_1_2CodeAction.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,89 @@ | ||
/******************************************************************************* | ||
* 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.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.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.services.extensions.ICodeActionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.IComponentProvider; | ||
import org.eclipse.lsp4xml.settings.XMLFormattingOptions; | ||
import org.eclipse.lsp4xml.utils.XMLPositionUtility; | ||
|
||
/** | ||
* Code action to fix cvc-attribute-3 error. | ||
* | ||
*/ | ||
public class src_import_1_2CodeAction implements ICodeActionParticipant { | ||
|
||
@Override | ||
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
XMLFormattingOptions formattingSettings, IComponentProvider componentProvider) { | ||
|
||
try { | ||
String prefix = document.getDocumentElement().getPrefix(); | ||
CodeAction namespace = createNamespaceCodeAction(diagnostic, range, document, prefix); | ||
CodeAction targetNamespace = createTargetNamespaceCodeAction(diagnostic, document, prefix); | ||
|
||
if (namespace != null) { | ||
codeActions.add(namespace); | ||
} | ||
|
||
if (targetNamespace != null) { | ||
codeActions.add(targetNamespace); | ||
} | ||
|
||
} catch (BadLocationException e) { | ||
// Do nothing | ||
} | ||
} | ||
|
||
private CodeAction createNamespaceCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, String prefix) throws BadLocationException { | ||
int offset = document.offsetAt(range.getStart()); | ||
DOMNode node = document.findNodeAt(offset); | ||
|
||
if (node == null || !node.isElement()) { | ||
return null; | ||
} | ||
|
||
String message; | ||
if (prefix != null) { | ||
message = "Insert 'namespace' attribute in '" + prefix + ":import' element"; | ||
} else { | ||
message = "Insert 'namespace' attribute in 'import' element"; | ||
} | ||
|
||
return CodeActionFactory.insert(message, range.getEnd(), " namespace=\"\"", document.getTextDocument(), diagnostic); | ||
} | ||
|
||
private CodeAction createTargetNamespaceCodeAction(Diagnostic diagnostic, DOMDocument document, String prefix) throws BadLocationException { | ||
DOMElement root = document.getDocumentElement(); | ||
|
||
if (!root.getTagName().contains("schema")) { | ||
return null; | ||
} | ||
String message; | ||
if (prefix != null) { | ||
message = "Insert 'targetNamespace' attribute in '" + prefix + ":schema' element"; | ||
} else { | ||
message = "Insert 'targetNamespace' attribute in 'schema' element"; | ||
} | ||
return CodeActionFactory.insert(message, document.positionAt(root.getStartTagCloseOffset()), " targetNamespace=\"\"", document.getTextDocument(), diagnostic); | ||
} | ||
|
||
} |
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