Search in sources :

Example 96 with Preferences

use of org.eclipse.core.runtime.Preferences in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method appendDelimBefore.

protected void appendDelimBefore(IJSONNode node, CompoundRegion toAppend, StringBuilder source) {
    if (node == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    String delim = getLineDelimiter(node);
    boolean needIndent = !(node instanceof IJSONDocument);
    if (toAppend == null) {
        source.append(delim);
        source.append(getIndent(node));
        if (needIndent)
            source.append(getIndentString());
    } else {
        String type = toAppend.getType();
        if (type == JSONRegionContexts.JSON_COMMENT) {
            RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
            it.prev();
            ITextRegion prev = it.prev();
            int[] result = null;
            if (prev == null || (prev.getType() == JSONRegionContexts.WHITE_SPACE && (result = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0))[0] >= 0)) {
                // Collapse to one empty line if there's more than one.
                if (result != null) {
                    int offset = result[0] + DefaultLineTracker.DELIMITERS[result[1]].length();
                    if (offset < it.getStructuredDocumentRegion().getText(prev).length()) {
                        if (TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), offset)[0] >= 0) {
                            source.append(delim);
                        }
                    }
                    source.append(delim);
                    source.append(getIndent(node));
                    if (needIndent)
                        source.append(getIndentString());
                }
            } else if (prev.getType() == JSONRegionContexts.JSON_COMMENT) {
                String fullText = toAppend.getDocumentRegion().getFullText(prev);
                String trimmedText = toAppend.getDocumentRegion().getText(prev);
                // $NON-NLS-1$
                String whiteSpaces = "";
                if (fullText != null && trimmedText != null)
                    whiteSpaces = fullText.substring(trimmedText.length());
                int[] delimiterFound = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, whiteSpaces, 0);
                if (delimiterFound[0] != -1) {
                    source.append(delim);
                } else {
                    appendSpaceBefore(node, toAppend.getText(), source);
                    /*
						 * If two comments can't be adjusted in one
						 * line(combined length exceeds line width), a tab is
						 * also appended along with next line delimiter , we
						 * need to remove that.
						 */
                    if (source.toString().endsWith(getIndentString())) {
                        source.delete((source.length() - getIndentString().length()), source.length());
                    }
                }
            } else {
                appendSpaceBefore(node, toAppend.getText(), source);
            }
        } else if (type == JSONRegionContexts.JSON_COMMA) {
            RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
            it.prev();
            ITextRegion prev = it.prev();
            Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
            if (prev.getType() == JSONRegionContexts.WHITE_SPACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
                source.append(delim);
                source.append(getIndent(node));
                if (needIndent)
                    source.append(getIndentString());
            } else if (preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != IJSONNode.PAIR_NODE)) {
                int length = getLastLineLength(node, source);
                int append = 1;
                if (length + append > preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH)) {
                    source.append(getLineDelimiter(node));
                    source.append(getIndent(node));
                    if (needIndent)
                        source.append(getIndentString());
                }
            }
        } else if (type == JSONRegionContexts.JSON_OBJECT_OPEN || type == JSONRegionContexts.JSON_OBJECT_CLOSE || type == JSONRegionContexts.JSON_ARRAY_OPEN || type == JSONRegionContexts.JSON_ARRAY_CLOSE) {
            source.append(delim);
            source.append(getIndent(node));
        } else {
            source.append(delim);
            source.append(getIndent(node));
            if (needIndent)
                source.append(getIndentString());
        }
    }
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) RegionIterator(org.eclipse.wst.json.core.internal.util.RegionIterator) Preferences(org.eclipse.core.runtime.Preferences)

Example 97 with Preferences

use of org.eclipse.core.runtime.Preferences in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method appendSpaceBefore.

protected void appendSpaceBefore(IJSONNode node, CompoundRegion toAppend, StringBuilder source) {
    if (node == null || toAppend == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    String type = toAppend.getType();
    Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
    boolean needIndent = !(node instanceof IJSONDocument);
    /*if (type == JSONRegionContexts.JSON_COMMENT) {
			// check whether previous region is 'S' and has CR-LF
			String delim = getLineDelimiter(node);
			RegionIterator it = new RegionIterator(
					toAppend.getDocumentRegion(), toAppend.getTextRegion());
			it.prev();
			ITextRegion prev = it.prev();
			// bug390904
			if (prev.getType() == JSONRegionContexts.JSON_LBRACE
					&& TextUtilities
							.indexOf(DefaultLineTracker.DELIMITERS,
									it.getStructuredDocumentRegion()
											.getFullText(prev), 0)[0] > 0) {
				source.append(delim);
				source.append(getIndent(node));
				source.append(getIndentString());
			} else if (prev.getType() == JSONRegionContexts.WHITE_SPACE
					&& TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it
							.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
				source.append(delim);
				source.append(getIndent(node));
				if (needIndent)
					source.append(getIndentString());
			} else {
				appendSpaceBefore(node, toAppend.getText(), source);
			}
		}*/
    if ((type == JSONRegionContexts.JSON_OBJECT_OPEN || type == JSONRegionContexts.JSON_ARRAY_OPEN) && preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
        String delim = getLineDelimiter(node);
        source.append(delim);
        source.append(getIndent(node));
    // } else if (type == JSONRegionContexts.JSON_CURLY_BRACE_CLOSE) {
    // } else if (type == JSONRegionContexts.JSON_INCLUDES || type ==
    // JSONRegionContexts.JSON_DASHMATCH) {
    /*} else if (type == JSONRegionContexts.JSON_DECLARATION_SEPARATOR
				&& node instanceof IJSONStyleDeclItem) {
			int n = preferences
					.getInt(JSONCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
			// no delimiter case
			while (n-- > 0)
				source.append(" ");//$NON-NLS-1$
		} else if (type == JSONRegionContexts.JSON_DECLARATION_VALUE_OPERATOR
				|| type == JSONRegionContexts.JSON_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
			if (preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH) > 0
					&& (!preferences
							.getBoolean(JSONCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node
							.getOwnerDocument().getNodeType() != IJSONNode.STYLEDECLARATION_NODE)) {
				int length = getLastLineLength(node, source);
				int append = 1;
				if (length + append > preferences
						.getInt(JSONCorePreferenceNames.LINE_WIDTH)) {
					source.append(getLineDelimiter(node));
					source.append(getIndent(node));
					if (needIndent)
						source.append(getIndentString());
				}
			}
		} else if (JSONRegionContexts.JSON_FOREIGN_ELEMENT == type
				|| JSONRegionContexts.JSON_DECLARATION_DELIMITER == type) {
			return;
		*/
    } else
        appendSpaceBefore(node, toAppend.getText(), source);
}
Also used : IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) Preferences(org.eclipse.core.runtime.Preferences)

Example 98 with Preferences

use of org.eclipse.core.runtime.Preferences in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method appendSpaceBefore.

protected void appendSpaceBefore(IJSONNode node, String toAppend, StringBuilder source) {
    if (node == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
    if (toAppend != null && toAppend.startsWith("{") && preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
        // $NON-NLS-1$
        source.append(getLineDelimiter(node));
        source.append(getIndent(node));
        return;
    } else if (/* ! mgr.isOnePropertyPerLine() && */
    preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR))) {
        int n = getLastLineLength(node, source);
        int append = (toAppend != null) ? TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, toAppend, 0)[0] : 0;
        if (toAppend != null)
            append = (append < 0) ? toAppend.length() : append;
        if (n + append + 1 > preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH)) {
            source.append(getLineDelimiter(node));
            source.append(getIndent(node));
            source.append(getIndentString());
            return;
        }
    }
    // whitespace to avoid the whitespace duplication.
    if (!(source.length() > 0 && source.toString().charAt(source.length() - 1) == ' ') && !(toAppend.length() > 0 && toAppend.charAt(toAppend.length() - 1) == ' ')) {
        // $NON-NLS-1$
        source.append(" ");
    }
}
Also used : Preferences(org.eclipse.core.runtime.Preferences)

Example 99 with Preferences

use of org.eclipse.core.runtime.Preferences in project webtools.sourceediting by eclipse.

the class AbstractJSONSourceFormatter method decoratedRegion.

protected String decoratedRegion(CompoundRegion region, int type, IJSONCleanupStrategy stgy) {
    if (isFormat())
        return region.getText();
    Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
    String text = null;
    if (!stgy.isFormatSource())
        text = region.getFullText();
    else
        text = region.getText();
    return text;
}
Also used : Preferences(org.eclipse.core.runtime.Preferences)

Example 100 with Preferences

use of org.eclipse.core.runtime.Preferences in project webtools.sourceediting by eclipse.

the class NewTagWizard method performFinish.

public boolean performFinish() {
    boolean performedOK = false;
    // save user options for next use
    fNewFileTemplatesPage.saveLastSavedPreferences();
    // no file extension specified so add default extension
    String fileName = fNewFilePage.getFileName();
    if (fileName.lastIndexOf('.') == -1) {
        String newFileName = fNewFilePage.addDefaultExtension(fileName);
        fNewFilePage.setFileName(newFileName);
    }
    // create a new empty file
    IFile file = fNewFilePage.createNewFile();
    // sure to check
    if (file != null) {
        if (!file.isLinked()) {
            // put template contents into file
            String templateString = fNewFileTemplatesPage.getTemplateString();
            if (templateString != null) {
                templateString = applyLineDelimiter(file, templateString);
                // determine the encoding for the new file
                Preferences preference = JSPCorePlugin.getDefault().getPluginPreferences();
                String charSet = preference.getString(CommonEncodingPreferenceNames.OUTPUT_CODESET);
                try {
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    OutputStreamWriter outputStreamWriter = null;
                    if (charSet == null || charSet.trim().equals("")) {
                        // $NON-NLS-1$
                        // just use default encoding
                        outputStreamWriter = new OutputStreamWriter(outputStream);
                    } else {
                        outputStreamWriter = new OutputStreamWriter(outputStream, charSet);
                    }
                    outputStreamWriter.write(templateString);
                    outputStreamWriter.flush();
                    outputStreamWriter.close();
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
                    file.setContents(inputStream, true, false, null);
                    inputStream.close();
                } catch (Exception e) {
                    // $NON-NLS-1$
                    Logger.log(Logger.WARNING_DEBUG, "Could not create contents for new Tag file", e);
                }
            }
        }
        // open the file in editor
        openEditor(file);
        // everything's fine
        performedOK = true;
    }
    return performedOK;
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Preferences(org.eclipse.core.runtime.Preferences) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

Preferences (org.eclipse.core.runtime.Preferences)113 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)7 IFile (org.eclipse.core.resources.IFile)6 PartInitException (org.eclipse.ui.PartInitException)6 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)6 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)5 IStructuredFormatPreferences (org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences)5 ArrayList (java.util.ArrayList)4 CoreException (org.eclipse.core.runtime.CoreException)4 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)4 IStructuredCleanupPreferences (org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences)4 StructuredCleanupPreferences (org.eclipse.wst.sse.core.internal.cleanup.StructuredCleanupPreferences)4 List (java.util.List)3 Vector (java.util.Vector)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2