All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2011 itemis AG (http://www.itemis.eu) 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
 *******************************************************************************/
package org.eclipse.xtext.xbase.ui.contentassist;

import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.util.ReplaceRegion;
import org.eclipse.xtext.xbase.imports.RewritableImportSection;
import org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender;
import org.eclipse.xtext.xbase.ui.imports.ReplaceConverter;

import com.google.inject.Inject;

/**
 * An {@link org.eclipse.xtext.xbase.compiler.IAppendable}?to insert text into an Xtend document. Takes imports and
 * existing variable names into account.
 * 
 * @author Jan Koehnlein - Initial contribution and API
 */
public class ReplacingAppendable extends DocumentSourceAppender {

	public static class Factory extends DocumentSourceAppender.Factory {

		@Inject
		private ReplaceConverter replaceConverter;
		
		@Override
		protected ReplacingAppendable newInstance(IXtextDocument document, RewritableImportSection importSection, WhitespaceHelper whitespaceHelper,
				String indentString, String lineSeparator, int baseIndentationLevel, boolean isJava) {
			return new ReplacingAppendable(document, importSection, whitespaceHelper, indentString, lineSeparator, baseIndentationLevel, isJava, replaceConverter);
		}
	}

	private ReplaceConverter replaceConverter;
	
	protected ReplacingAppendable(IXtextDocument document, RewritableImportSection importSection, WhitespaceHelper whitespaceHelper,
			String indentString, String lineSeparator, int baseIndentationLevel, boolean isJava, ReplaceConverter replaceConverter) {
		super(document, importSection, whitespaceHelper, indentString, lineSeparator, baseIndentationLevel, isJava);
		this.replaceConverter = replaceConverter;
	}
	
	public int commitChanges() throws BadLocationException {
		ReplaceRegion change = getChange();
		return commitChanges(change);
	}
	
	public int commitChanges(int offset, int length) throws BadLocationException {
		ReplaceRegion change = getChange(offset, length);
		return commitChanges(change);
	}

	protected int commitChanges(ReplaceRegion change) throws BadLocationException {
		getDocument().replace(change.getOffset(), change.getLength(), change.getText());
		return insertNewImports();
	}
	
	public int insertNewImports() throws BadLocationException {
		List importChanges = getImportSection().rewrite();
		int lengthDelta = 0;
		if(!importChanges.isEmpty()) {
			for(ReplaceRegion change: importChanges) {
				lengthDelta = lengthDelta - change.getLength() + change.getText().length(); 
			}
			replaceConverter.convertToTextEdit(importChanges).apply(getDocument());
		}
		return lengthDelta;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy