use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class AbstractSmokeTest method testSkipNodesInBetween.
@Test
public void testSkipNodesInBetween() throws Exception {
for (String string : getSmokeTestModels()) {
LazyLinkingResource resource = createResource(string);
if (resource != null) {
ICompositeNode rootNode = resource.getParseResult().getRootNode();
ReplaceRegion replaceRegion = null;
for (INode node : rootNode.getAsTreeIterable()) {
int offset = node.getTotalOffset();
int length = node.getTotalLength();
if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
replaceRegion = new ReplaceRegion(offset, length, "");
StringBuilder builder = new StringBuilder(string);
replaceRegion.applyTo(builder);
processModel(builder.toString());
}
}
}
}
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class ReplacingAppendable method insertNewImports.
public int insertNewImports() throws BadLocationException {
List<ReplaceRegion> 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;
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class DocumentRewriter method getChanges.
public List<ReplaceRegion> getChanges() {
List<ReplaceRegion> changes = newArrayList();
for (Section section : sections) {
ReplaceRegion change = section.getChange();
changes.add(change);
}
for (ReplaceRegion change : importSection.rewrite()) {
changes.add(change);
}
return changes;
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class MultiImportOrganizer method organizeImports.
/**
* @param files
* ordered by corresponding {@link IProject}
* @param mon
* @return Creates {@link Change}s for each {@link IFile} using {@link ImportOrganizer}
* @throws InterruptedException
*/
public List<Change> organizeImports(Multimap<IProject, IFile> files, IProgressMonitor mon) throws InterruptedException {
List<Change> result = Lists.newArrayList();
for (IProject project : files.keySet()) {
ResourceSet resSet = resSetProvider.get(project);
for (IFile file : files.get(project)) {
mon.subTask("Calculating imports in - " + file.getName());
URI uri = storageMapper.getUri(file);
if (uri != null) {
XtextResource resource = (XtextResource) resSet.getResource(uri, true);
List<ReplaceRegion> replace = importOrganizer.getOrganizedImportChanges(resource);
// TODO - find out why \n\n changes are produced, even if there are any unused imports
if (replace == null) {
continue;
}
TextEdit textEdit = replaceConverter.convertToTextEdit(replace);
IRefactoringDocument iRefactoringDocument = provider.get(uri, status);
Change change = iRefactoringDocument.createChange(Messages.OrganizeImports, textEdit);
if (change instanceof EditorDocumentChange) {
if (((EditorDocument) iRefactoringDocument).getEditor().isDirty()) {
((EditorDocumentChange) change).setDoSave(false);
}
}
result.add(change);
}
mon.worked(1);
if (mon.isCanceled()) {
throw new InterruptedException();
}
}
}
return result;
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class OrganizeImportsHandler method doOrganizeImports.
public void doOrganizeImports(final IXtextDocument document) {
List<ReplaceRegion> result = document.priorityReadOnly(new IUnitOfWork<List<ReplaceRegion>, XtextResource>() {
@Override
public List<ReplaceRegion> exec(XtextResource state) throws Exception {
return importOrganizer.getOrganizedImportChanges(state);
}
});
if (result == null || result.isEmpty())
return;
try {
DocumentRewriteSession session = null;
if (document instanceof IDocumentExtension4) {
session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
}
replaceConverter.convertToTextEdit(result).apply(document);
if (session != null) {
((IDocumentExtension4) document).stopRewriteSession(session);
}
} catch (BadLocationException e) {
LOG.error(Messages.OrganizeImportsHandler_organizeImportsErrorMessage, e);
}
}
Aggregations