Search in sources :

Example 1 with ChangeLogEditor

use of org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor 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();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ChangeLogEditor(org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor) ChangeLogWriter(org.eclipse.linuxtools.internal.changelog.core.ChangeLogWriter) IFormatterChangeLogContrib(org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib) IEditorPart(org.eclipse.ui.IEditorPart) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TeamException(org.eclipse.team.core.TeamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with ChangeLogEditor

use of org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor in project linuxtools by eclipse.

the class GNUFormat method mergeChangelog.

@Override
public String mergeChangelog(String dateLine, String functionGuess, String defaultContent, IEditorPart changelog, String changeLogLocation, String fileLocation) {
    String fileDetail = formatFileDetail(changeLogLocation, fileLocation);
    IDocument changelog_doc = getDocument(changelog);
    String function = formatFunction(functionGuess);
    boolean multipleEntrySuccess = false;
    boolean forceNewEntry = false;
    // $NON-NLS-1$
    String functionSpacer = " ";
    if (// $NON-NLS-1$
    function.equals(": "))
        // $NON-NLS-1$
        functionSpacer = "";
    /* Fix Bz #366854.  Make sure that forceNewEntry is used only
         * once and then cleared even when the ChangeLog is empty to start with.
         */
    if (changelog instanceof ChangeLogEditor) {
        ChangeLogEditor editor = (ChangeLogEditor) changelog;
        forceNewEntry = editor.isForceNewLogEntry();
        editor.setForceNewLogEntry(false);
    }
    if (changelog_doc.getLength() > 0) {
        int offset_start = findChangeLogEntry(changelog_doc, dateLine);
        int offset_end = dateLine.length();
        boolean foundFunction = false;
        // written to a new entry again.
        if (forceNewEntry) {
            offset_start = -1;
        }
        if (offset_start != -1) {
            int nextChangeEntry = findChangeLogPattern(changelog_doc, offset_start + dateLine.length());
            int functLogEntry = offset_start + dateLine.length();
            final int numLines = changelog_doc.getNumberOfLines();
            while (functLogEntry < nextChangeEntry) {
                int lineNum = 0;
                // $NON-NLS-1$
                String entry = "";
                try {
                    lineNum = changelog_doc.getLineOfOffset(functLogEntry);
                    entry = changelog_doc.get(functLogEntry, changelog_doc.getLineLength(lineNum));
                } catch (BadLocationException e) {
                // Should never get here
                }
                // Look to see if entry already exists for file (will be preceded by "*")
                final int entryStart = entry.indexOf("* " + fileDetail);
                if (entryStart >= 0) {
                    foundFunction = true;
                } else if (foundFunction && isFileLine(entry)) {
                    functLogEntry--;
                    break;
                }
                if (foundFunction) {
                    foundFunction = true;
                    // In such a case, just return.  We don't need to repeat ourselves.
                    if (defaultContent.length() > 0 && entry.lastIndexOf(defaultContent) > 0) {
                        // $NON-NLS-1$
                        return "";
                    }
                    final int nextFunctLoc;
                    if (entryStart > 0) {
                        nextFunctLoc = functLogEntry + entryStart + fileDetail.length() + 2;
                    } else {
                        nextFunctLoc = functLogEntry;
                    }
                    // $NON-NLS-1$
                    String nextFunc = "";
                    try {
                        final int lineEnd;
                        if (lineNum < numLines - 1) {
                            lineEnd = changelog_doc.getLineOffset(lineNum + 1) - 1;
                        } else {
                            lineEnd = changelog_doc.getLength();
                        }
                        nextFunc = changelog_doc.get(nextFunctLoc, lineEnd - nextFunctLoc);
                    } catch (BadLocationException e1) {
                    // Should never get here
                    }
                    if (nextFunc.trim().startsWith(function)) {
                        // $NON-NLS-1$
                        return "";
                    }
                }
                try {
                    functLogEntry += changelog_doc.getLineLength(lineNum);
                } catch (BadLocationException e1) {
                // Should never get here
                }
            }
            if (functLogEntry >= nextChangeEntry) {
                functLogEntry = nextChangeEntry - 1;
                try {
                    // Get rid of some potential lines containing whitespace only.
                    functLogEntry = removeWhitespaceOnlyLines(changelog_doc, functLogEntry);
                    while (// $NON-NLS-1$
                    changelog_doc.get(functLogEntry, 1).equals("\n")) functLogEntry--;
                } catch (BadLocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                functLogEntry++;
            }
            if (offset_start != -1) {
                if (foundFunction) {
                    try {
                        if (// $NON-NLS-1$
                        !function.equals(": "))
                            changelog_doc.replace(functLogEntry, 0, // $NON-NLS-1$
                            "\n" + TAB + function + // $NON-NLS-1$
                            " ");
                        else
                            // $NON-NLS-1$
                            changelog_doc.replace(// $NON-NLS-1$
                            functLogEntry, // $NON-NLS-1$
                            0, // $NON-NLS-1$
                            "\n" + TAB);
                    } catch (BadLocationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ITextEditor edit = (ITextEditor) changelog;
                    if (// $NON-NLS-1$
                    !function.equals(": "))
                        edit.selectAndReveal(functLogEntry + function.length() + 3, 0);
                    else
                        edit.selectAndReveal(functLogEntry + function.length(), 0);
                    multipleEntrySuccess = true;
                } else {
                    try {
                        changelog_doc.replace(offset_end, 0, TAB + "* " + fileDetail + // $NON-NLS-1$
                        functionSpacer + function + functionSpacer + defaultContent + // $NON-NLS-1$
                        "\n");
                    } catch (BadLocationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ITextEditor edit = (ITextEditor) changelog;
                    edit.selectAndReveal(offset_end + fileDetail.length() + function.length() + functionSpacer.length() * 2 + 3 + defaultContent.length(), 0);
                    multipleEntrySuccess = true;
                }
            }
        }
    }
    if (!multipleEntrySuccess) {
        try {
            if (changelog_doc.getLength() > 0) {
                // $NON-NLS-1$
                changelog_doc.replace(0, 0, "\n\n");
            }
            changelog_doc.replace(0, 0, // $NON-NLS-1$
            dateLine + TAB + "* " + fileDetail + functionSpacer + function + functionSpacer + defaultContent);
            ITextEditor edit = (ITextEditor) changelog;
            edit.selectAndReveal(dateLine.length() + fileDetail.length() + function.length() + functionSpacer.length() * 2 + 3 + defaultContent.length(), 0);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
    // $NON-NLS-1$
    return "";
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ChangeLogEditor(org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)2 ChangeLogEditor (org.eclipse.linuxtools.internal.changelog.core.editors.ChangeLogEditor)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 IPath (org.eclipse.core.runtime.IPath)1 IDocument (org.eclipse.jface.text.IDocument)1 IFormatterChangeLogContrib (org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib)1 ChangeLogWriter (org.eclipse.linuxtools.internal.changelog.core.ChangeLogWriter)1 TeamException (org.eclipse.team.core.TeamException)1 IEditorPart (org.eclipse.ui.IEditorPart)1 PartInitException (org.eclipse.ui.PartInitException)1 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)1