use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.
the class SpecfileCompletionProcessor method getDefines.
/**
* Get defines as a String key->value pair for a given specfile and prefix.
*
* @param specfile
* to get defines from.
* @param prefix
* used to find defines.
* @return a <code>HashMap</code> of defines.
*/
private Map<String, String> getDefines(Specfile specfile, String prefix) {
Collection<SpecfileDefine> defines = specfile.getDefines();
Map<String, String> ret = new HashMap<>();
String defineName;
for (SpecfileDefine define : defines) {
// $NON-NLS-1$
defineName = "%" + define.getName();
if (defineName.startsWith(prefix.replaceFirst("\\{", EMPTY_STRING))) {
// $NON-NLS-1$
ret.put(defineName, define.getStringValue());
}
}
return ret;
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.
the class SpecfileHover method getHoverInfo.
@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
if (hoverRegion == null || hoverRegion.getLength() == 0) {
return null;
}
Specfile spec = new SpecfileParser().parse(textViewer.getDocument());
String currentSelection;
try {
currentSelection = textViewer.getDocument().get(hoverRegion.getOffset() + 1, hoverRegion.getLength() - 1);
} catch (BadLocationException e) {
return null;
}
// First we try to get a define based on the given name
SpecfileDefine define = spec.getDefine(currentSelection);
// $NON-NLS-1$
String value = currentSelection + ": ";
if (define != null) {
return value + define.getStringValue();
}
String macroLower = currentSelection.toLowerCase();
// If there's no such define we try to see if it corresponds to
// a Source or Patch declaration
String retrivedValue = RPMUtils.getSourceOrPatchValue(spec, macroLower);
if (retrivedValue != null) {
return value + retrivedValue;
} else {
// If it does not correspond to a Patch or Source macro, try to find
// it
// in the macro proposals list.
retrivedValue = RPMUtils.getMacroValueFromMacroList(currentSelection);
if (retrivedValue != null) {
return value + retrivedValue;
} else {
// If it does not correspond to a macro in the list, try to find
// it
// in the RPM list.
retrivedValue = Activator.getDefault().getRpmPackageList().getValue(// $NON-NLS-1$ //$NON-NLS-2$
currentSelection.replaceFirst(":", ""));
if (retrivedValue != null) {
return retrivedValue;
}
}
}
// for unrecognized macros and RPM packages.
return null;
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.
the class UiUtils method resolveDefines.
/**
* Resolve defines for a given string. Defines in the string that are not
* found or have some other error will remain unchanged in the returned
* string.
*
* @param specfile
* The specfile containing the string to resolve.
* @param stringToResolve
* The string to resolve.
* @return The resolved string.
*/
public static String resolveDefines(Specfile specfile, String stringToResolve) {
String workingString = stringToResolve;
SpecfileDefine define;
try {
// $NON-NLS-1$
Pattern variablePattern = Pattern.compile("%\\{(\\S+?)\\}");
Matcher variableMatcher = variablePattern.matcher(stringToResolve);
Set<String> variablesFound = new HashSet<>();
while (variableMatcher.find()) {
String variable = variableMatcher.group(1);
if (variablesFound.contains(variable)) {
continue;
}
define = specfile.getDefine(variable);
if (define != null && !stringToResolve.equals(define.getUnresolvedStringValue())) {
// $NON-NLS-1$ //$NON-NLS-2$
workingString = workingString.replaceAll("\\%\\{" + variable + "\\}", define.getStringValue());
}
}
return workingString;
} catch (Exception e) {
return stringToResolve;
}
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.
the class SpecfileElementHyperlinkDetector method detectHyperlinks.
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
if (region == null || textViewer == null) {
return null;
}
IDocument document = textViewer.getDocument();
if (document == null) {
return null;
}
// until a SpecfileEditor#editorSaved is called
if (specfile == null) {
SpecfileEditor a = this.getAdapter(SpecfileEditor.class);
if (a != null && a.getSpecfile() != null) {
specfile = a.getSpecfile();
} else {
SpecfileParser parser = new SpecfileParser();
specfile = parser.parse(document);
}
}
int offset = region.getOffset();
IRegion lineInfo;
String line;
try {
lineInfo = document.getLineInformationOfOffset(offset);
line = document.get(lineInfo.getOffset(), lineInfo.getLength());
} catch (BadLocationException ex) {
return null;
}
int offsetInLine = offset - lineInfo.getOffset();
StringTokenizer tokens = new StringTokenizer(line);
// $NON-NLS-1$
String word = "";
int tempLineOffset = 0;
int wordOffsetInLine = 0;
while (tokens.hasMoreTokens()) {
String tempWord = tokens.nextToken();
// $NON-NLS-1$
Pattern defineRegexp = Pattern.compile("%\\{(.*?)\\}");
Matcher fit = defineRegexp.matcher(tempWord);
while (fit.find()) {
if ((fit.start() + tempLineOffset <= offsetInLine) && (offsetInLine <= fit.end() + tempLineOffset)) {
tempWord = fit.group();
wordOffsetInLine = fit.start();
tempLineOffset += fit.start();
break;
}
}
tempLineOffset += tempWord.length();
word = tempWord;
if (tempLineOffset > offsetInLine) {
break;
}
}
if (word.startsWith(SOURCE_IDENTIFIER)) {
int sourceNumber = Integer.valueOf(word.substring(SOURCE_IDENTIFIER.length(), word.length() - 1)).intValue();
SpecfileSource source = specfile.getSource(sourceNumber);
if (source != null) {
return prepareHyperlink(lineInfo, line, word, source);
}
} else if (word.startsWith(PATCH_IDENTIFIER)) {
int sourceNumber = Integer.valueOf(word.substring(PATCH_IDENTIFIER.length(), word.length())).intValue();
SpecfileSource source = specfile.getPatch(sourceNumber);
if (source != null) {
return prepareHyperlink(lineInfo, line, word, source);
}
} else {
String defineName = getDefineName(word);
SpecfileDefine define = specfile.getDefine(defineName);
if (define != null) {
return prepareHyperlink(lineInfo, line, defineName, define, wordOffsetInLine);
}
}
return null;
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine in project linuxtools by eclipse.
the class SpecfileElementHyperlinkDetectorTest method testDetectHyperlinks.
@Test
public void testDetectHyperlinks() throws PartInitException {
String testText = "%define smth other\nSource0: test.zip\nPatch0: first.patch\n" + "%build\n %{SOURCE0}\n%patch0\n%{smth}\n";
newFile(testText);
SpecfileElementHyperlinkDetector elementDetector = new SpecfileElementHyperlinkDetector();
elementDetector.setSpecfile(specfile);
IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), testFile, "org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor");
editor = (SpecfileEditor) openEditor;
editor.doRevertToSaved();
// test source element
IRegion region = new Region(74, 0);
IHyperlink[] returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
SpecfileElementHyperlink element = (SpecfileElementHyperlink) returned[0];
assertTrue(element.getSource() instanceof SpecfileSource);
SpecfileSource source = (SpecfileSource) element.getSource();
assertEquals(source.getSourceType(), SpecfileSource.SourceType.SOURCE);
assertEquals(source.getFileName(), "test.zip");
// test patch element
region = new Region(83, 0);
returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
element = (SpecfileElementHyperlink) returned[0];
assertTrue(element.getSource() instanceof SpecfileSource);
source = (SpecfileSource) element.getSource();
assertEquals(source.getSourceType(), SpecfileSource.SourceType.PATCH);
assertEquals(source.getFileName(), "first.patch");
// test define
region = new Region(89, 0);
returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
element = (SpecfileElementHyperlink) returned[0];
assertTrue(element.getSource() instanceof SpecfileDefine);
SpecfileDefine define = (SpecfileDefine) element.getSource();
assertEquals(define.getName(), "smth");
assertEquals(define.getStringValue(), "other");
}
Aggregations