com.vladsch.flexmark.ast.DelimitedNode Java Examples

The following examples show how to use com.vladsch.flexmark.ast.DelimitedNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmartFormat.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Collects the text of a single paragraph.
 *
 * Replaces:
 *   - tabs with spaces
 *   - newlines with spaces (may occur in Code nodes)
 *   - soft line breaks with spaces
 *   - hard line breaks with special marker characters
 *   - spaces and tabs in special nodes, that should not formatted, with marker characters
 */
private void collectFormattableText(StringBuilder buf, Node node) {
	for (Node n = node.getFirstChild(); n != null; n = n.getNext()) {
		if (n instanceof Text) {
			buf.append(n.getChars().toString().replace('\t', ' ').replace('\n', ' '));
		} else if (n instanceof DelimitedNode) {
			// italic, bold and code
			buf.append(((DelimitedNode) n).getOpeningMarker());
			collectFormattableText(buf, n);
			buf.append(((DelimitedNode) n).getClosingMarker());
		} else if (n instanceof SoftLineBreak) {
			buf.append(' ');
		} else if (n instanceof HardLineBreak) {
			buf.append(' ').append(n.getChars().startsWith("\\")
				? HARD_LINE_BREAK_BACKSLASH : HARD_LINE_BREAK_SPACES).append(' ');
		} else {
			// other text that should be not wrapped or formatted
			buf.append(protectWhitespace(n.getChars().toString()));
		}
	}
}
 
Example #2
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void surroundSelectionAndReplaceMarker(String leading, String trailing, String hint,
		DelimitedNode node, String newOpeningMarker, String newClosingMarker)
{
	IndexRange selection = textArea.getSelection();
	int start = selection.getStart();
	int end = selection.getEnd();

	String selectedText = textArea.getSelectedText();

	// remove leading and trailing whitespaces from selected text
	String trimmedSelectedText = selectedText.trim();
	if (trimmedSelectedText.length() < selectedText.length()) {
		start += selectedText.indexOf(trimmedSelectedText);
		end = start + trimmedSelectedText.length();
	}

	BasedSequence openingMarker = node.getOpeningMarker();
	BasedSequence closingMarker = node.getClosingMarker();

	int selStart = start + leading.length() + (newOpeningMarker.length() - openingMarker.length());
	int selEnd = selStart + trimmedSelectedText.length();

	// insert hint text if selection is empty
	if (hint != null && trimmedSelectedText.isEmpty()) {
		trimmedSelectedText = hint;
		selEnd = selStart + hint.length();
	}

	// replace text and update selection
	// Note: using single textArea.replaceText() to avoid multiple changes in undo history
	String before = textArea.getText(openingMarker.getEndOffset(), start);
	String after = textArea.getText(end, closingMarker.getStartOffset());
	replaceText(textArea, openingMarker.getStartOffset(), closingMarker.getEndOffset(),
			newOpeningMarker + before + leading + trimmedSelectedText + trailing + after + newClosingMarker );
	selectRange(textArea, selStart, selEnd);
}
 
Example #3
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void insertDelimited(Class<? extends Node> cls, String openCloseMarker, String hint) {
	List<? extends Node> nodes = findNodesAtSelection((s, e, n) -> cls.isInstance(n), false, false);
	if (nodes.size() > 0) {
		// there is delimited text in current selection --> change them to plain text
		if (nodes.size() == 1 && hint.equals(((DelimitedNode)nodes.get(0)).getText().toString())) {
			// delete node including hint text
			Node node = nodes.get(0);
			deleteText(textArea, node.getStartOffset(), node.getEndOffset());
		} else
			removeDelimiters(nodes);
	} else
		surroundSelectionInCode(openCloseMarker, hint);
}
 
Example #4
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private <T extends Node> void removeDelimiters(List<T> nodes) {
	StringBuilder buf = new StringBuilder();
	for (int i = 0; i < nodes.size(); i++) {
		T node = nodes.get(i);
		if (i > 0)
			buf.append(textArea.getText(nodes.get(i - 1).getEndOffset(), node.getStartOffset()));
		buf.append(((DelimitedNode)node).getText());
	}

	int start = nodes.get(0).getStartOffset();
	int end = nodes.get(nodes.size() - 1).getEndOffset();
	replaceText(textArea, start, end, buf.toString());
	selectRange(textArea, start, start + buf.length());
}
 
Example #5
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Find single node that completely encloses the current selection and match a predicate.
 */
private <T extends Node> T findNodeAtSelection(FindNodePredicate predicate) {
	IndexRange selection = textArea.getSelection();
	int start = selection.getStart();
	int end = selection.getEnd();
	List<T> nodes = findNodes(start, end, predicate, false, false);
	if (nodes.size() != 1)
		return null;

	T node = nodes.get(0);
	BasedSequence text = (node instanceof DelimitedNode) ? ((DelimitedNode)node).getText() : node.getChars();
	return (start >= text.getStartOffset() && end <= text.getEndOffset()) ? node : null;
}