Search in sources :

Example 1 with IRefactoringDocument

use of org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument in project xtext-eclipse by eclipse.

the class RefactoringDocumentProviderTest method testFileDocument.

@Test
public void testFileDocument() throws Exception {
    IRefactoringDocument document = createAndCheckDocument(testFile);
    assertTrue(document instanceof FileDocument);
    assertEquals(testFile, ((FileDocument) document).getFile());
    assertEquals(TEST_FILE_CONTENT, document.getOriginalContents());
    Change change = document.createChange(CHANGE_NAME, textEdit);
    assertTrue(change instanceof TextFileChange);
    assertEquals(CHANGE_NAME, change.getName());
    Change undoChange = checkEdit(document, textEdit);
    assertNotNull(undoChange);
}
Also used : FileDocument(org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.FileDocument) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) Change(org.eclipse.ltk.core.refactoring.Change) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Example 2 with IRefactoringDocument

use of org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument in project xtext-eclipse by eclipse.

the class RefactoringDocumentProviderTest method createAndCheckDocument.

protected IRefactoringDocument createAndCheckDocument(IFile testFile) {
    IRefactoringDocument document = documentFactory.get(URI.createPlatformResourceURI(testFile.getFullPath().toString(), true), status);
    assertTrue(status.getRefactoringStatus().isOK());
    assertNotNull(document);
    return document;
}
Also used : IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument)

Example 3 with IRefactoringDocument

use of org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument 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 || replace.isEmpty()) {
                    continue;
                }
                TextEdit textEdit = replaceConverter.convertToTextEdit(replace);
                IRefactoringDocument iRefactoringDocument = provider.get(uri, status);
                if (iRefactoringDocument != null) {
                    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;
}
Also used : IFile(org.eclipse.core.resources.IFile) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Change(org.eclipse.ltk.core.refactoring.Change) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TextEdit(org.eclipse.text.edits.TextEdit)

Example 4 with IRefactoringDocument

use of org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument in project xtext-eclipse by eclipse.

the class RefactoringDocumentProviderTest method testCleanEditorDocument.

@Test
public void testCleanEditorDocument() throws Exception {
    XtextEditor editor = openEditor(testFile);
    assertFalse(editor.isDirty());
    IRefactoringDocument cleanDocument = createAndCheckDocument(testFile);
    assertTrue(cleanDocument instanceof EditorDocument);
    IXtextDocument editorDocument = editor.getDocument();
    assertEquals(editorDocument, ((EditorDocument) cleanDocument).getDocument());
    assertEquals(TEST_FILE_CONTENT, cleanDocument.getOriginalContents());
    Change change = cleanDocument.createChange(CHANGE_NAME, textEdit);
    assertTrue(change instanceof EditorDocumentChange);
    assertEquals(TEST_FILE_NAME + " - " + TEST_PROJECT, change.getName());
    assertEquals(editor, ((EditorDocumentChange) change).getEditor());
    assertTrue(((EditorDocumentChange) change).isDoSave());
    Change undoChange = checkEdit(cleanDocument, textEdit);
    assertNotNull(undoChange);
    IRefactoringDocument dirtyDocument = createAndCheckDocument(testFile);
    assertTrue(cleanDocument instanceof EditorDocument);
    assertEquals(editorDocument, ((EditorDocument) dirtyDocument).getDocument());
}
Also used : EditorDocument(org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.EditorDocument) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) Change(org.eclipse.ltk.core.refactoring.Change) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Example 5 with IRefactoringDocument

use of org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument in project xtext-eclipse by eclipse.

the class RefactoringDocumentProviderTest method testDirtyEditorDocument.

@Test
public void testDirtyEditorDocument() throws Exception {
    XtextEditor editor = openEditor(testFile);
    editor.getDocument().replace(0, 0, " ");
    editor.getDocument().replace(0, 1, "");
    assertTrue(editor.isDirty());
    IRefactoringDocument cleanDocument = createAndCheckDocument(testFile);
    assertTrue(cleanDocument instanceof EditorDocument);
    IXtextDocument editorDocument = editor.getDocument();
    assertEquals(editorDocument, ((EditorDocument) cleanDocument).getDocument());
    assertEquals(TEST_FILE_CONTENT, cleanDocument.getOriginalContents());
    Change change = cleanDocument.createChange(CHANGE_NAME, textEdit);
    assertTrue(change instanceof EditorDocumentChange);
    assertEquals(TEST_FILE_NAME + " - " + TEST_PROJECT, change.getName());
    assertEquals(editor, ((EditorDocumentChange) change).getEditor());
    assertFalse(((EditorDocumentChange) change).isDoSave());
    Change undoChange = checkEdit(cleanDocument, textEdit);
    assertNotNull(undoChange);
    IRefactoringDocument dirtyDocument = createAndCheckDocument(testFile);
    assertTrue(cleanDocument instanceof EditorDocument);
    assertEquals(editorDocument, ((EditorDocument) dirtyDocument).getDocument());
}
Also used : EditorDocument(org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.EditorDocument) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) Change(org.eclipse.ltk.core.refactoring.Change) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Aggregations

IRefactoringDocument (org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument)5 Change (org.eclipse.ltk.core.refactoring.Change)4 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)4 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)3 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)3 Test (org.junit.Test)3 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 EditorDocument (org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.EditorDocument)2 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 URI (org.eclipse.emf.common.util.URI)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 TextEdit (org.eclipse.text.edits.TextEdit)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 FileDocument (org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.FileDocument)1 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)1