Skip to content

Commit

Permalink
Add syntax highlight from adapted from bnd-tools
Browse files Browse the repository at this point in the history
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
laeubi committed Nov 8, 2023
1 parent 9d0c379 commit 1822935
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 37 deletions.
1 change: 1 addition & 0 deletions ui/org.eclipse.pde.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Require-Bundle:
org.eclipse.equinox.security;bundle-version="[1.4.100,2.0.0)"
Import-Package: aQute.bnd.build.model;version="[4.2.0,5.0.0)",
aQute.bnd.header;version="[2.5.0,3.0.0)",
aQute.bnd.help;version="2.0.0",
aQute.bnd.osgi;version="[5.6.0,8.0.0)",
aQute.bnd.osgi.repository;version="[3.0.0,4.0.0)",
aQute.bnd.osgi.resource;version="[4.3.0,6.0.0)",
Expand Down
16 changes: 15 additions & 1 deletion ui/org.eclipse.pde.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2478,8 +2478,22 @@
<extension
point="org.eclipse.ui.genericeditor.contentAssistProcessors">
<contentAssistProcessor
class="org.eclipse.pde.internal.ui.editor.bnd.BndAutoCompleteProcessor"
class="org.eclipse.pde.internal.ui.editor.bnd.BndBuildPathAutoCompleteProcessor"
contentType="org.eclipse.pde.bndInstructions">
</contentAssistProcessor>
</extension>
<extension
point="org.eclipse.ui.genericeditor.contentAssistProcessors">
<contentAssistProcessor
class="org.eclipse.pde.internal.ui.bndtools.BndCompletionProcessor"
contentType="org.eclipse.pde.bndInstructions">
</contentAssistProcessor>
</extension>
<extension
point="org.eclipse.ui.genericeditor.presentationReconcilers">
<presentationReconciler
class="org.eclipse.pde.internal.ui.editor.bnd.BNDPresentationReconciler"
contentType="org.eclipse.pde.bndInstructions">
</presentationReconciler>
</extension>
</plugin>
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
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
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
Expand All @@ -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)
Expand All @@ -55,7 +74,7 @@ public BndScanner(BndSourceViewerConfiguration manager) {
};

setRules(rules);
setDefaultReturnToken(bsvc.T_DEFAULT);
setDefaultReturnToken(T_DEFAULT);
}

IToken comment(ICharacterScanner scanner) {
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -145,7 +163,7 @@ IToken error(ICharacterScanner scanner) {
c = scanner.read();
}
scanner.unread();
return bsvc.T_ERROR;
return T_ERROR;
}
}
while (n-- > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import aQute.bnd.properties.LineType;
import aQute.bnd.properties.PropertiesLineReader;

public class BndAutoCompleteProcessor implements IContentAssistProcessor {
public class BndBuildPathAutoCompleteProcessor implements IContentAssistProcessor {

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
Expand Down

0 comments on commit 1822935

Please sign in to comment.