-
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.
Add support for
textDocument/documentHighlight
for DTD
Fixes #545 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
5961c2d
commit 9f0b5fd
Showing
4 changed files
with
295 additions
and
5 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
98 changes: 98 additions & 0 deletions
98
...main/java/org/eclipse/lsp4xml/extensions/dtd/participants/DTDHighlightingParticipant.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,98 @@ | ||
/******************************************************************************* | ||
* 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.dtd.participants; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.DocumentHighlight; | ||
import org.eclipse.lsp4j.DocumentHighlightKind; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.eclipse.lsp4xml.dom.DOMNode; | ||
import org.eclipse.lsp4xml.dom.DTDAttlistDecl; | ||
import org.eclipse.lsp4xml.dom.DTDDeclParameter; | ||
import org.eclipse.lsp4xml.dom.DTDElementDecl; | ||
import org.eclipse.lsp4xml.extensions.dtd.utils.DTDUtils; | ||
import org.eclipse.lsp4xml.services.extensions.IHighlightingParticipant; | ||
import org.eclipse.lsp4xml.utils.XMLPositionUtility; | ||
|
||
/** | ||
* DTD highlight participant | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
|
||
public class DTDHighlightingParticipant implements IHighlightingParticipant { | ||
|
||
@Override | ||
public void findDocumentHighlights(DOMNode node, Position position, int offset, List<DocumentHighlight> highlights, | ||
CancelChecker cancelChecker) { | ||
boolean findReferences = false; | ||
DTDDeclParameter parameter = null; | ||
DTDElementDecl elementDecl = null; | ||
|
||
if (node.isDTDElementDecl()) { | ||
elementDecl = (DTDElementDecl) node; | ||
if (elementDecl.isInNameParameter(offset)) { | ||
// <!ELEMENT na|me --> here cursor is in the name of <!ELEMENT | ||
// we must find all references from the <!ELEMENT which defines the name | ||
findReferences = true; | ||
parameter = elementDecl.getNameParameter(); | ||
} else { | ||
// <!ELEMENT name (chi|ld --> here cursor is in the child element | ||
// we must find only the <!ELEMENT child | ||
parameter = elementDecl.getParameterAt(offset); | ||
} | ||
} else if (node.isDTDAttListDecl()) { | ||
DTDAttlistDecl attlistDecl = (DTDAttlistDecl) node; | ||
if (attlistDecl.isInNameParameter(offset)) { | ||
// <!ATTLIST na|me --> here cusror is in the name of <!ATTLIST | ||
// we must find only the <!ELEMENT name | ||
parameter = attlistDecl.getNameParameter(); | ||
} | ||
} | ||
|
||
if (parameter == null) { | ||
return; | ||
} | ||
|
||
if (findReferences) { | ||
// case with <!ELEMENT na|me | ||
|
||
// highlight <!ELEMENT na|me | ||
DTDDeclParameter originNode = parameter; | ||
highlights.add( | ||
new DocumentHighlight(XMLPositionUtility.createRange(originNode), DocumentHighlightKind.Write)); | ||
|
||
// highlight all references of na|me in ATTLIST and child of <!ELEMENT | ||
DTDUtils.searchDTDOriginElementDecls(elementDecl, (origin, target) -> { | ||
highlights | ||
.add(new DocumentHighlight(XMLPositionUtility.createRange(origin), DocumentHighlightKind.Read)); | ||
}, cancelChecker); | ||
} else { | ||
// case with | ||
// - <!ELEMENT name (chi|ld | ||
// - <!ATTLIST na|me | ||
|
||
// highlight <!ELEMENT name (chi|ld or <!ATTLIST na|me | ||
DTDDeclParameter targetNode = parameter; | ||
highlights | ||
.add(new DocumentHighlight(XMLPositionUtility.createRange(targetNode), DocumentHighlightKind.Read)); | ||
|
||
// highlight the target <!ELEMENT nam|e | ||
DTDUtils.searchDTDTargetElementDecl(parameter, true, targetName -> { | ||
highlights.add( | ||
new DocumentHighlight(XMLPositionUtility.createRange(targetName), DocumentHighlightKind.Write)); | ||
}); | ||
} | ||
} | ||
|
||
} |
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