Skip to main content
summaryrefslogtreecommitdiffstats
blob: 548a13c5229d704e53d6cc833b27c930afb5c4ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Benjamin Muskalla - initial API and implementation - https://bugs.eclipse.org/bugs/show_bug.cgi?id=41573
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import java.util.ResourceBundle;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;


/**
 * Action for joining two or more lines together by deleting the
 * line delimiters and trimming the whitespace between them.
 *
 * @since 3.3
 */
public class JoinLinesAction extends TextEditorAction {

	private String fJoint= null;


	/**
	 * Creates a line joining action.
	 *
	 * @param bundle the resource bundle for UI strings
	 * @param prefix the prefix for the property keys into <code>bundle</code>
	 * @param editor the editor
	 * @param joint the string to put between the lines
	 */
	public JoinLinesAction(ResourceBundle bundle, String prefix, ITextEditor editor, String joint) {
		super(bundle, prefix, editor);
		Assert.isLegal(joint != null);
		fJoint= joint;
		update();
	}

	@Override
	public void run() {

		ITextEditor editor= getTextEditor();
		if (editor == null)
			return;

		if (!validateEditorInputState())
			return;

		IDocument document= getDocument(editor);
		if (document == null)
			return;

		ITextSelection selection= getSelection(editor);
		if (selection == null)
			return;

		int startLine= selection.getStartLine();
		int endLine= selection.getEndLine();
		try {
			int caretOffset= joinLines(document, startLine, endLine);
			if (caretOffset > -1)
				editor.selectAndReveal(caretOffset, 0);
		} catch (BadLocationException e) {
			// should not happen
		}

	}

	/**
	 * Returns the editor's document.
	 *
	 * @param editor the editor
	 * @return the editor's document
	 */
	private static IDocument getDocument(ITextEditor editor) {

		IDocumentProvider documentProvider= editor.getDocumentProvider();
		if (documentProvider == null)
			return null;

		IDocument document= documentProvider.getDocument(editor.getEditorInput());
		if (document == null)
			return null;

		return document;
	}

	/**
	 * Returns the editor's selection.
	 *
	 * @param editor the editor
	 * @return the editor's selection
	 */
	private static ITextSelection getSelection(ITextEditor editor) {

		ISelectionProvider selectionProvider= editor.getSelectionProvider();
		if (selectionProvider == null)
			return null;

		ISelection selection= selectionProvider.getSelection();
		if (!(selection instanceof ITextSelection))
			return null;

		return (ITextSelection) selection;
	}

	@Override
	public void update() {
		super.update();
		if (!isEnabled())
			return;

		if (!canModifyEditor()) {
			setEnabled(false);
			return;
		}

		ITextEditor editor= getTextEditor();
		setEnabled(editor.isEditable());
	}

	/**
	 * Joins several text lines to one line.
	 *
	 * @param document the document
	 * @param startLine the start line
	 * @param endLine the end line
	 * @return the new caret offset
	 * @throws BadLocationException if the document is accessed with wrong line or offset
	 */
	private int joinLines(IDocument document, int startLine, int endLine) throws BadLocationException {
		if (startLine == document.getNumberOfLines() - 1) {
			// do nothing because we are in the last line
			return -1;
		}

		if (startLine == endLine)
			endLine++; // append join with the next line

		StringBuilder buffer= new StringBuilder();
		for (int line= startLine; line <= endLine; line++) {
			buffer.append(trim(document, line, line == startLine));
			if (line != endLine)
				buffer.append(fJoint);
		}

		int startLineOffset= document.getLineOffset(startLine);
		int endLineOffset= document.getLineOffset(endLine)	+ document.getLineLength(endLine) - getLineDelimiterLength(document, endLine);
		String replaceString= buffer.toString();
		document.replace(startLineOffset, endLineOffset - startLineOffset, replaceString);
		return startLineOffset + replaceString.length();
	}

	private String trim(IDocument document, int line, boolean ignoreLeadingWhitespace) throws BadLocationException {
		int lineOffset= document.getLineOffset(line);
		int lineLength= document.getLineLength(line);
		lineLength= lineLength - getLineDelimiterLength(document, line);
		if (!ignoreLeadingWhitespace)
			return document.get(lineOffset, lineLength).trim();

		while (lineLength > 0 && Character.isWhitespace(document.getChar(lineOffset + lineLength - 1)))
			lineLength--;

		return document.get(lineOffset, lineLength);
	}

	private int getLineDelimiterLength(IDocument document, int line) throws BadLocationException {
		String lineDelimiter= document.getLineDelimiter(line);
		return lineDelimiter != null ? lineDelimiter.length() : 0;

	}

}

Back to the top