use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor 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.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.
the class URLHyperlinkWithMacroDetector method detectHyperlinks.
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
if (specfile == null) {
SpecfileEditor a = this.getAdapter(SpecfileEditor.class);
if (a != null) {
specfile = a.getSpecfile();
} else {
return null;
}
}
IHyperlink[] returned = super.detectHyperlinks(textViewer, region, canShowMultipleHyperlinks);
if (returned != null && returned.length > 0) {
IHyperlink hyperlink = returned[0];
if (hyperlink instanceof URLHyperlink) {
URLHyperlink urlHyperlink = (URLHyperlink) hyperlink;
String newURLString = UiUtils.resolveDefines(specfile, urlHyperlink.getURLString());
return new IHyperlink[] { new URLHyperlink(urlHyperlink.getHyperlinkRegion(), newURLString) };
}
}
return returned;
}
use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.
the class FileTestCase method setUp.
@Before
public void setUp() throws CoreException {
testProject = new SpecfileTestProject();
String fileName = "test" + this.getClass().getSimpleName() + ".spec";
testFile = testProject.createFile(fileName);
editor = new SpecfileEditor();
parser = new SpecfileParser();
specfile = new Specfile();
}
use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.
the class SpecfileCompletionProcessorTest method initEditor.
private SpecfileEditor initEditor(String contents) throws Exception {
newFile(contents);
IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), testFile);
return (SpecfileEditor) openEditor;
}
use of org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor in project linuxtools by eclipse.
the class SpecfileChangelogFormatter method mergeChangelog.
@Override
public String mergeChangelog(String dateLine, String functionGuess, String defaultContent, IEditorPart changelog, String changeLogLocation, String fileLocation) {
if (changelog instanceof SpecfileEditor) {
SpecfileEditor specEditor = (SpecfileEditor) changelog;
IDocument doc = specEditor.getDocumentProvider().getDocument(specEditor.getEditorInput());
String[] positionCategories = doc.getPositionCategories();
String contentTypesPositionCategory = null;
// we need to find the one we want
for (String positionCategory : positionCategories) {
if (positionCategory.startsWith("__content_types_category")) {
// $NON-NLS-1$
contentTypesPositionCategory = positionCategory;
}
}
if (contentTypesPositionCategory != null) {
try {
Position[] sectionPositions = doc.getPositions(contentTypesPositionCategory);
ITypedRegion changelogPartition = null;
for (Position position : sectionPositions) {
int offset = position.getOffset();
ITypedRegion partition = doc.getPartition(offset);
if (partition.getType().equals(SpecfilePartitionScanner.SPEC_CHANGELOG)) {
changelogPartition = partition;
}
}
// Temporary buffer for changelog text
StringBuilder buf = new StringBuilder();
String changelogText = EMPTY_STRING;
String[] changelogLines = new String[] {};
int offset = doc.getLength();
int length = 0;
// there was no changelog partition add it.
if (changelogPartition == null) {
// make sure there are at least 2 newlines before
// the changelog section
String endString = doc.get(doc.getLength() - 2, 2);
if (endString.charAt(0) != '\n') {
buf.append('\n');
}
if (endString.charAt(1) != '\n') {
buf.append('\n');
}
// $NON-NLS-1$
buf.append("%changelog\n");
// or get the old text and add the header
} else {
offset = changelogPartition.getOffset();
length = changelogPartition.getLength();
changelogText = doc.get(offset, length);
// get old changelog text
// $NON-NLS-1$
changelogLines = changelogText.split("\n");
// add the %changelog header
buf.append(changelogLines[0]).append('\n');
}
// now add the entry stub
buf.append(dateLine);
buf.append('\n');
// $NON-NLS-1$
buf.append("- \n");
// set the cursor at the end of the entry,
// count back 2 '\n's
int newCursorOffset = offset + buf.length() - 1;
for (int i = 1; i < changelogLines.length; i++) {
buf.append('\n').append(changelogLines[i]);
}
// always terminate the file with a new line
if (changelogLines.length > 0) {
buf.append('\n');
}
doc.replace(offset, length, buf.toString());
specEditor.selectAndReveal(newCursorOffset, 0);
specEditor.setFocus();
} catch (BadPositionCategoryException | BadLocationException e) {
SpecfileLog.logError(e);
}
}
}
return EMPTY_STRING;
}
Aggregations