-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax highlight from adapted from bnd-tools
Currently there is no syntax-highlight when PDE shows the bnd instruction files. This connects the highlight/code completion form the bndtools project with the source page to provide completion and syntax coloring. Fix #701
- Loading branch information
Showing
7 changed files
with
102 additions
and
37 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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2011, 2022 bndtools project. | ||
* Copyright (c) 2011, 2023 bndtools project and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
|
@@ -13,15 +13,18 @@ | |
* Ferry Huberts <[email protected]> - ongoing enhancements | ||
* Neil Bartlett <[email protected]> - ongoing enhancements | ||
* BJ Hargrave <[email protected]> - ongoing enhancements | ||
* Christoph Läubrich - adjust to coding conventions, fix missing completions on empty lines | ||
*******************************************************************************/ | ||
package bndtools.editor.completion; | ||
package org.eclipse.pde.internal.ui.bndtools; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.jface.text.ITextViewer; | ||
import org.eclipse.jface.text.contentassist.CompletionProposal; | ||
import org.eclipse.jface.text.contentassist.ContextInformation; | ||
|
@@ -34,24 +37,27 @@ | |
|
||
public class BndCompletionProcessor implements IContentAssistProcessor { | ||
|
||
private static final Pattern PREFIX_PATTERN = Pattern.compile("(\\S+)$"); | ||
private static final Pattern PREFIX_PATTERN = Pattern.compile("(\\S+)$"); //$NON-NLS-1$ | ||
|
||
@Override | ||
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { | ||
try { | ||
String pre = viewer.getDocument() | ||
.get(0, offset); | ||
Matcher matcher = PREFIX_PATTERN.matcher(pre); | ||
if (matcher.find()) { | ||
String prefix = matcher.group(1); | ||
ICompletionProposal[] found = proposals(prefix, offset); | ||
if (found.length == 1) { | ||
found[0].apply(viewer.getDocument()); | ||
viewer.setSelectedRange(offset + (found[0].getDisplayString() | ||
.length() - prefix.length() + 2), 0); | ||
return new ICompletionProposal[0]; | ||
IDocument document = viewer.getDocument(); | ||
IRegion lineInfo = document.getLineInformationOfOffset(offset); | ||
if (lineInfo.getOffset() != offset) { | ||
String pre = document.get(0, offset); | ||
Matcher matcher = PREFIX_PATTERN.matcher(pre); | ||
if (matcher.find()) { | ||
String prefix = matcher.group(1); | ||
ICompletionProposal[] found = proposals(prefix, offset); | ||
if (found.length == 1) { | ||
found[0].apply(document); | ||
viewer.setSelectedRange(offset + (found[0].getDisplayString().length() - prefix.length() + 2), | ||
0); | ||
return new ICompletionProposal[0]; | ||
} | ||
return found; | ||
} | ||
return found; | ||
} | ||
return proposals(null, offset); | ||
} catch (BadLocationException e) { | ||
|
@@ -79,7 +85,6 @@ private static ICompletionProposal[] proposals(String prefix, int offset) { | |
|
||
@Override | ||
public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
|
@@ -99,13 +104,11 @@ public char[] getContextInformationAutoActivationCharacters() { | |
|
||
@Override | ||
public IContextInformationValidator getContextInformationValidator() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getErrorMessage() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2011, 2023 bndtools project. | ||
* Copyright (c) 2011, 2023 bndtools project and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
|
@@ -15,29 +15,48 @@ | |
* BJ Hargrave <[email protected]> - ongoing enhancements | ||
* Amit Kumar Mondal <[email protected]> - ongoing enhancements | ||
* Peter Kriens <[email protected]> - ongoing enhancements | ||
* Christoph Läubrich - incline code from BndSourceViewerConfiguration regarding colors, adjust to eclipse coding conventions | ||
*******************************************************************************/ | ||
package bndtools.editor.completion; | ||
package org.eclipse.pde.internal.ui.bndtools; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.ui.text.IColorManager; | ||
import org.eclipse.jdt.ui.text.IJavaColorConstants; | ||
import org.eclipse.jface.text.TextAttribute; | ||
import org.eclipse.jface.text.rules.ICharacterScanner; | ||
import org.eclipse.jface.text.rules.IRule; | ||
import org.eclipse.jface.text.rules.IToken; | ||
import org.eclipse.jface.text.rules.RuleBasedScanner; | ||
import org.eclipse.jface.text.rules.Token; | ||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.graphics.RGB; | ||
|
||
import aQute.bnd.help.Syntax; | ||
import aQute.bnd.osgi.Constants; | ||
|
||
public class BndScanner extends RuleBasedScanner { | ||
BndSourceViewerConfiguration bsvc; | ||
final Set<String> instructions; | ||
final Set<String> directives = new HashSet<>(); | ||
|
||
public BndScanner(BndSourceViewerConfiguration manager) { | ||
bsvc = manager; | ||
private Token T_DEFAULT; | ||
private Token T_KEY; | ||
private Token T_ERROR; | ||
private Token T_COMMENT; | ||
private Token T_INSTRUCTION; | ||
private Token T_OPTION; | ||
|
||
public BndScanner(IColorManager colorManager) { | ||
T_DEFAULT = new Token(new TextAttribute(colorManager.getColor(IJavaColorConstants.JAVA_DEFAULT))); | ||
T_KEY = new Token(new TextAttribute(colorManager.getColor(IJavaColorConstants.JAVADOC_LINK), null, SWT.NONE)); | ||
T_ERROR = new Token(new TextAttribute(colorManager.getColor(IJavaColorConstants.JAVA_KEYWORD), | ||
colorManager.getColor(new RGB(255, 0, 0)), SWT.BOLD)); | ||
T_COMMENT = new Token(new TextAttribute(colorManager.getColor(IJavaColorConstants.JAVA_SINGLE_LINE_COMMENT))); | ||
T_INSTRUCTION = new Token( | ||
new TextAttribute(colorManager.getColor(IJavaColorConstants.JAVADOC_KEYWORD), null, SWT.BOLD)); | ||
T_OPTION = new Token( | ||
new TextAttribute(colorManager.getColor(IJavaColorConstants.JAVADOC_LINK), null, SWT.BOLD)); | ||
instructions = Syntax.HELP.values() | ||
.stream() | ||
.map(Syntax::getHeader) | ||
|
@@ -55,7 +74,7 @@ public BndScanner(BndSourceViewerConfiguration manager) { | |
}; | ||
|
||
setRules(rules); | ||
setDefaultReturnToken(bsvc.T_DEFAULT); | ||
setDefaultReturnToken(T_DEFAULT); | ||
} | ||
|
||
IToken comment(ICharacterScanner scanner) { | ||
|
@@ -76,13 +95,12 @@ IToken comment(ICharacterScanner scanner) { | |
n++; | ||
|
||
if (c == '\n' || c == '\r' || c == ICharacterScanner.EOF) | ||
return bsvc.T_COMMENT; | ||
return T_COMMENT; | ||
} | ||
} else { | ||
while (n-- > 0) | ||
scanner.unread(); | ||
return Token.UNDEFINED; | ||
} | ||
while (n-- > 0) | ||
scanner.unread(); | ||
return Token.UNDEFINED; | ||
} | ||
} | ||
|
||
|
@@ -124,14 +142,14 @@ IToken keyword(ICharacterScanner scanner) { | |
String key = sb.toString(); | ||
|
||
if (Constants.options.contains(key)) { | ||
return bsvc.T_OPTION; | ||
return T_OPTION; | ||
} | ||
|
||
if (instructions.contains(key)) { | ||
return bsvc.T_INSTRUCTION; | ||
return T_INSTRUCTION; | ||
} | ||
|
||
return bsvc.T_KEY; | ||
return T_KEY; | ||
} | ||
|
||
IToken error(ICharacterScanner scanner) { | ||
|
@@ -145,7 +163,7 @@ IToken error(ICharacterScanner scanner) { | |
c = scanner.read(); | ||
} | ||
scanner.unread(); | ||
return bsvc.T_ERROR; | ||
return T_ERROR; | ||
} | ||
} | ||
while (n-- > 0) | ||
|
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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
* Neil Bartlett <[email protected]> - ongoing enhancements | ||
* BJ Hargrave <[email protected]> - ongoing enhancements | ||
*******************************************************************************/ | ||
package bndtools.editor.completion; | ||
package org.eclipse.pde.internal.ui.bndtools; | ||
|
||
import org.eclipse.jface.text.rules.ICharacterScanner; | ||
import org.eclipse.jface.text.rules.IRule; | ||
|
29 changes: 29 additions & 0 deletions
29
....eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/bnd/BNDPresentationReconciler.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,29 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Christoph Läubrich and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Christoph Läubrich - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.pde.internal.ui.editor.bnd; | ||
|
||
import org.eclipse.jdt.ui.JavaUI; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.presentation.PresentationReconciler; | ||
import org.eclipse.jface.text.rules.DefaultDamagerRepairer; | ||
import org.eclipse.pde.internal.ui.bndtools.BndScanner; | ||
|
||
public class BNDPresentationReconciler extends PresentationReconciler { | ||
public BNDPresentationReconciler() { | ||
BndScanner scanner = new BndScanner(JavaUI.getColorManager()); | ||
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(scanner); | ||
this.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); | ||
this.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); | ||
} | ||
} |
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