use of org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib in project linuxtools by eclipse.
the class ChangeLogExtensionManager method getFormatterContributor.
/**
* Fetches formatterName formatter from extension, but if there exists a inline
* formatter for entryFileName, then it uses that inline formatter.
* @param entryFilePath The full file name.
* @param formatterName The formatter name.
* @return The changelog formatter.
*/
public IFormatterChangeLogContrib getFormatterContributor(String entryFilePath, String formatterName) {
// extract just file name;
String fileName;
int lastDir = entryFilePath.lastIndexOf('/');
if ((lastDir >= 0) && (lastDir + 1 <= entryFilePath.length()))
fileName = entryFilePath.substring(lastDir + 1, entryFilePath.length());
else
fileName = entryFilePath;
// We don't yet know which formatter to use
formatterConfigElementToUse = null;
if (formatterExtensions != null) {
IConfigurationElement[] elements = formatterExtensions.getConfigurationElements();
// cache the in-file formatters on the first run
if (cachedInFileFormateters == null) {
List<IConfigurationElement> inFileFormatters = new LinkedList<>();
for (int i = 0; i < elements.length; i++) {
IConfigurationElement formatterConfigElement = elements[i];
if (// $NON-NLS-1$
formatterConfigElement.getName().equals("formatter") && // $NON-NLS-1$
formatterConfigElement.getAttribute("inFile").equalsIgnoreCase("true")) {
// $NON-NLS-1$
inFileFormatters.add(elements[i]);
}
}
cachedInFileFormateters = inFileFormatters.toArray(new IConfigurationElement[] {});
}
// edited file
for (int i = 0; i < cachedInFileFormateters.length; i++) {
IConfigurationElement formatterConfigElement = cachedInFileFormateters[i];
IConfigurationElement[] patternElementTmp = formatterConfigElement.getChildren();
// error check
if (patternElementTmp == null)
continue;
IConfigurationElement patternElement = patternElementTmp[0];
if (patternElement.getAttribute("pattern") == null) {
// $NON-NLS-1$
ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, // $NON-NLS-1$
Messages.getString("ChangeLog.ErrNonPattern"), // $NON-NLS-1$
new Exception(Messages.getString("ChangeLog.ErrNonPattern"))));
} else {
// $NON-NLS-1$
String filePattern = patternElement.getAttribute("pattern");
try {
Pattern pattern = Pattern.compile(filePattern);
Matcher fileMatcher = pattern.matcher(fileName);
// pattern then we're done
if (fileMatcher.matches()) {
formatterConfigElementToUse = formatterConfigElement;
break;
}
} catch (PatternSyntaxException e) {
ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));
}
}
}
// prefered formatter
if (formatterConfigElementToUse == null) {
// whenever it changes
if (cachedPrefFormatter == null || !// $NON-NLS-1$
cachedPrefFormatter.getAttribute("name").equals(formatterName)) {
for (int i = 0; i < elements.length; i++) {
IConfigurationElement formatterConfigElement = elements[i];
if (formatterConfigElement.getName().equals("formatter") && formatterConfigElement.getAttribute("inFile").equalsIgnoreCase("false")) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (// $NON-NLS-1$
formatterConfigElement.getAttribute("name").equals(formatterName))
cachedPrefFormatter = formatterConfigElement;
break;
}
}
}
formatterConfigElementToUse = cachedPrefFormatter;
if (formatterConfigElementToUse == null) {
ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, // $NON-NLS-1$
Messages.getString("ChangeLog.ErrRetrieveFormatter"), // $NON-NLS-1$
new Exception(Messages.getString("ChangeLog.ErrRetrieveFormatter"))));
return null;
}
}
}
try {
return (IFormatterChangeLogContrib) formatterConfigElementToUse.createExecutableExtension(// $NON-NLS-1$
"class");
} catch (CoreException e) {
ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));
e.printStackTrace();
}
return null;
}
use of org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib in project linuxtools by eclipse.
the class PrepareChangeLogAction method outputMultipleEntryChangeLog.
private void outputMultipleEntryChangeLog(PatchFile pf, String[] functionGuess) {
String defaultContent = null;
if (pf.isNewfile())
// $NON-NLS-1$
defaultContent = Messages.getString("ChangeLog.NewFile");
else if (pf.isRemovedFile())
// $NON-NLS-1$
defaultContent = Messages.getString("ChangeLog.RemovedFile");
IPath entryPath = pf.getPath();
String entryFileName = entryPath.toOSString();
ChangeLogWriter clw = new ChangeLogWriter();
// load settings from extensions + user pref.
loadPreferences();
// get file path from target file
clw.setEntryFilePath(entryPath.toOSString());
if (defaultContent != null)
clw.setDefaultContent(defaultContent);
// err check. do nothing if no file is being open/edited
if (clw.getEntryFilePath() == "") {
return;
}
// Check if formatter is internal or inline..if inline, use the
// current active editor part, otherwise, we must find the external
// ChangeLog file.
IEditorPart changelog = null;
// Before accessing the getFormatterConfigElement, the getFormatContibutor
// method must be called to initialize.
extensionManager.getFormatterContributor(clw.getEntryFilePath(), pref_Formatter);
IConfigurationElement formatterConfigElement = extensionManager.getFormatterConfigElement();
if (// $NON-NLS-1$
formatterConfigElement.getAttribute("inFile").equalsIgnoreCase("true")) {
// $NON-NLS-1$
try {
changelog = openEditor((IFile) pf.getResource());
clw.setFormatter(extensionManager.getFormatterContributor(clw.getEntryFilePath(), pref_Formatter));
} catch (Exception e) {
// do nothing changelog will be null
}
} else {
// external changelog
// get formatter
clw.setFormatter(extensionManager.getFormatterContributor(entryFileName, pref_Formatter));
if (pf.isRemovedFile())
changelog = getChangelogForRemovePath(entryPath);
else
changelog = getChangelog(entryFileName);
// one.
if (createChangeLog && changelog == null)
changelog = askChangeLogLocation(entryPath.toOSString());
if (changelog == null) {
createChangeLog = false;
return;
}
}
if ((changelog instanceof ChangeLogEditor) && (!this.newEntryWritten)) {
ChangeLogEditor editor = (ChangeLogEditor) changelog;
// saving), treat it as a change log modification
if (editor.isDirty())
this.changeLogModified = true;
editor.setForceNewLogEntry(!this.changeLogModified);
this.newEntryWritten = true;
}
// select changelog
clw.setChangelog(changelog);
// write to changelog
IFormatterChangeLogContrib formatter = clw.getFormatter();
clw.setDateLine(formatter.formatDateLine(pref_AuthorName, pref_AuthorEmail));
clw.setChangelogLocation(getDocumentLocation(clw.getChangelog(), true));
// print multiple changelog entries with different
// function guess names. We default to an empty guessed name
// if we have zero function guess names.
int numFuncs = 0;
// $NON-NLS-1$
clw.setGuessedFName("");
if (functionGuess.length > 0) {
for (String guess : functionGuess) {
if (!guess.trim().equals("")) {
// $NON-NLS-1$
++numFuncs;
clw.setGuessedFName(guess);
clw.writeChangeLog();
}
}
}
// function guesses.
if (numFuncs == 0) {
clw.writeChangeLog();
}
}
use of org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib in project linuxtools by eclipse.
the class ChangeLogWriterTest method testGetSetFormatter.
@Test
public void testGetSetFormatter() {
IFormatterChangeLogContrib formatter = new GNUFormat();
clogWriter.setFormatter(formatter);
assertEquals(formatter, clogWriter.getFormatter());
}
Aggregations