Skip to main content
summaryrefslogtreecommitdiffstats
blob: c997bae69d6d031ad6823d64f10aaf9746d79f0b (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
/*******************************************************************************
 * Copyright (c) 2017 Rogue Wave Software Inc. 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:
 *  Michał Niewrzał (Rogue Wave Software Inc.) - initial implementation
 *******************************************************************************/
package org.eclipse.ui.genericeditor.examples.dotproject;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;

public class TagAutoEditStrategy implements IAutoEditStrategy {

	@Override
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
		if (!">".equals(command.text)) { //$NON-NLS-1$
			return;
		}
		try {
			IRegion region = document.getLineInformationOfOffset(command.offset);
			String line = document.get(region.getOffset(), command.offset - region.getOffset());
			int index = line.lastIndexOf('<');
			if (index != -1 && (index != line.length() - 1) && line.charAt(index + 1) != '/') {
				String tag = line.substring(index + 1);
				command.text += "</" + tag + command.text; //$NON-NLS-1$
			}
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

}

Back to the top