use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser in project linuxtools by eclipse.
the class SpecfileCompletionProcessor method computeCompletionProposals.
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
List<ICompletionProposal> result = new ArrayList<>();
Specfile specfile = new SpecfileParser().parse(viewer.getDocument());
String prefix = completionWord(viewer.getDocument(), offset);
Region region = new Region(offset - prefix.length(), prefix.length());
// RPM macro's are useful in the whole specfile.
List<ICompletionProposal> rpmMacroProposals = computeRpmMacroProposals(region, specfile, prefix);
// Sources completion
List<ICompletionProposal> sourcesProposals = computeSourcesProposals(region, specfile, prefix);
result.addAll(sourcesProposals);
// Get the current content type
String currentContentType = viewer.getDocument().getDocumentPartitioner().getContentType(region.getOffset());
if (currentContentType.equals(SpecfilePartitionScanner.SPEC_PREP)) {
List<ICompletionProposal> patchesProposals = computePatchesProposals(region, specfile, prefix);
result.addAll(patchesProposals);
}
if (currentContentType.equals(SpecfilePartitionScanner.SPEC_PACKAGES)) {
// don't show template in the RPM packages content type.
// (when the line begin with Requires, BuildRequires etc...)
List<ICompletionProposal> rpmPackageProposals = computeRpmPackageProposals(region, prefix);
result.addAll(rpmPackageProposals);
result.addAll(rpmMacroProposals);
} else {
// don't show RPM packages proposals in all others content type.
List<? extends ICompletionProposal> templateProposals = computeTemplateProposals(viewer, region, specfile, prefix);
result.addAll(templateProposals);
result.addAll(rpmMacroProposals);
}
if (currentContentType.equals(SpecfilePartitionScanner.SPEC_GROUP)) {
IDocument document = viewer.getDocument();
try {
int lineNumber = document.getLineOfOffset(region.getOffset());
int lineOffset = document.getLineOffset(lineNumber);
if (region.getOffset() - lineOffset > 5) {
result.clear();
String groupPrefix = getGroupPrefix(viewer, offset);
result.addAll(computeRpmGroupProposals(region, groupPrefix));
}
} catch (BadLocationException e) {
SpecfileLog.logError(e);
}
}
return result.toArray(new ICompletionProposal[result.size()]);
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser 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.SpecfileParser in project linuxtools by eclipse.
the class SpecfileEditorPrepareSourcesActionDelegate method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final Shell shell = HandlerUtil.getActiveShellChecked(event);
final SpecfileParser specparser = new SpecfileParser();
final IResource resource = RPMHandlerUtils.getResource(event);
final RPMProject rpj = RPMHandlerUtils.getRPMProject(resource);
final IFile workFile = (IFile) rpj.getSpecFile();
final Specfile specfile = specparser.parse(workFile);
if (!downloadFile(shell, rpj, specfile)) {
return null;
}
Job job = new // $NON-NLS-1$
Job(// $NON-NLS-1$
"Preparing sources") {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask(NLS.bind(Messages.PrepareSources_prepareSources, rpj.getSpecFile().getName()), IProgressMonitor.UNKNOWN);
// $NON-NLS-1$
int offset = rpj.getSpecFile().getName().lastIndexOf(".");
MessageConsoleStream out = getConsole(rpj.getSpecFile().getName().substring(0, offset)).newMessageStream();
IStatus is = null;
try {
is = rpj.buildPrep(out);
} catch (CoreException e) {
SpecfileLog.logError(Messages.PrepareSources_coreException, e);
RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, Messages.PrepareSources_coreException);
return is;
} finally {
monitor.done();
}
return is;
}
};
// suppress UI. That's done in encapsulated
job.setUser(true);
job.schedule();
return null;
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser in project linuxtools by eclipse.
the class SpecStructureCreator method parseSpecfile.
private void parseSpecfile(DocumentRangeNode root, IDocument doc, IFile file) {
SpecfileParser parser = new SpecfileParser();
// FIXME: error markers do not show
if (file != null) {
FileEditorInput fei = new FileEditorInput(file);
// without it, the compare editor is blank
try {
SpecfileEditor.getSpecfileDocumentProvider().disconnect(fei);
SpecfileEditor.getSpecfileDocumentProvider().connect(fei);
} catch (CoreException e) {
SpecfileLog.logError(e);
}
parser.setErrorHandler(new SpecfileErrorHandler(fei, doc));
parser.setTaskHandler(new SpecfileTaskHandler(fei, doc));
Specfile specfile = parser.parse(doc);
String id = specfile.getName();
// Be a child under parent node of specfileSectionRoot (would be
// rootNode)
SpecNode fileNode = new SpecNode((DocumentRangeNode) root.getParentNode(), 1, id, doc, 0, doc.getLength());
for (SpecfileSection sec : specfile.getSections()) {
try {
addNode(root, doc, sec.getName(), doc.getLineOffset(sec.getLineNumber()), doc.getLineOffset(sec.getSectionEndLine()) - doc.getLineOffset(sec.getLineNumber()), 2);
} catch (BadLocationException e) {
SpecfileLog.logError(e);
}
}
// Be a child under the parent file node
for (SpecfilePackage sPackage : specfile.getPackages().getPackages()) {
try {
SpecNode pNode = addNode(fileNode, doc, sPackage.getPackageName(), doc.getLineOffset(sPackage.getLineNumber()), doc.getLineOffset(sPackage.getSectionEndLine()) - doc.getLineOffset(sPackage.getLineNumber()), 3);
for (SpecfileSection section : sPackage.getSections()) {
addNode(pNode, doc, section.getName(), doc.getLineOffset(section.getLineNumber()), doc.getLineOffset(section.getSectionEndLine()) - doc.getLineOffset(section.getLineNumber()), 4);
}
} catch (BadLocationException e) {
SpecfileLog.logError(e);
}
}
}
}
use of org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser 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;
}
Aggregations