Java Code Examples for org.eclipse.jface.text.Document#set()

The following examples show how to use org.eclipse.jface.text.Document#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OverrideCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) {
	CompilationUnit ast = CoreASTProvider.getInstance().getAST(fCompilationUnit, CoreASTProvider.WAIT_YES, null);
	if (ast != null) {
		recoveredDocument.set(document.get());
		return ast;
	}

	char[] content= document.get().toCharArray();

	// clear prefix to avoid compile errors
	int index= offset - 1;
	while (index >= 0 && Character.isJavaIdentifierPart(content[index])) {
		content[index]= ' ';
		index--;
	}

	recoveredDocument.set(new String(content));

	final ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
	parser.setResolveBindings(true);
	parser.setStatementsRecovery(true);
	parser.setSource(content);
	parser.setUnitName(fCompilationUnit.getElementName());
	parser.setProject(fCompilationUnit.getJavaProject());
	return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}
 
Example 2
Source File: SaveActionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testWillSaveWaitUntil() throws Exception {

	URI srcUri = project.getFile("src/java/Foo4.java").getRawLocationURI();
	ICompilationUnit cu = JDTUtils.resolveCompilationUnit(srcUri);

	StringBuilder buf = new StringBuilder();
	buf.append("package java;\n");
	buf.append("\n");
	buf.append("public class Foo4 {\n");
	buf.append("}\n");

	WillSaveTextDocumentParams params = new WillSaveTextDocumentParams();
	TextDocumentIdentifier document = new TextDocumentIdentifier();
	document.setUri(srcUri.toString());
	params.setTextDocument(document);

	List<TextEdit> result = handler.willSaveWaitUntil(params, monitor);

	Document doc = new Document();
	doc.set(cu.getSource());
	assertEquals(TextEditUtil.apply(doc, result), buf.toString());
}
 
Example 3
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public void testCython4() throws Exception {
    Document doc = new Document();
    doc.set(""
            + "def a():\n"
            + "    cdef int b\n"
            + "        def c():\n"
            + "    def d():\n"
            + "    def e():\n"
            + "        cdef int b\n"
            + "\n");
    List<stmtType> stmts = FastParser.parseCython(doc);

    String s = printAst(stmts);
    assertEquals(""
            + "def a(self):\n"
            + "    def int b(self):\n"
            + "        def c(self):\n"
            + "    def d(self):\n"
            + "    def e(self):\n"
            + "        def int b(self):\n"
            + "", s);
}
 
Example 4
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public void testGettingClassOrFunc() throws Exception {
    Document doc = new Document();
    doc.set("def bar(a):\n" +
            "\n" +
            "class \\\n" +
            "  Bar\n" +
            "    def mm\n" +
            "def\n" + //no space after
            "class\n" + //no space after
            "class \n" +
            "");

    List<stmtType> all = FastParser.parseClassesAndFunctions(doc);
    assertEquals(4, all.size());
    check(all, 0, 1, 1, 1, 5);
    check(all, 1, 3, 1, 3, 7);
    check(all, 2, 5, 5, 5, 9);
    check(all, 3, 8, 1, 8, 7);
}
 
Example 5
Source File: OverrideCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) {
	CompilationUnit ast= SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
	if (ast != null) {
		recoveredDocument.set(document.get());
		return ast;
	}

	char[] content= document.get().toCharArray();

	// clear prefix to avoid compile errors
	int index= offset - 1;
	while (index >= 0 && Character.isJavaIdentifierPart(content[index])) {
		content[index]= ' ';
		index--;
	}

	recoveredDocument.set(new String(content));

	final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	parser.setResolveBindings(true);
	parser.setStatementsRecovery(true);
	parser.setSource(content);
	parser.setUnitName(fCompilationUnit.getElementName());
	parser.setProject(fCompilationUnit.getJavaProject());
	return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}
 
Example 6
Source File: SelectionKeeperTest.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public void testSelectionKeeper2() throws Exception {
    Document doc = new Document();
    final String initial = "" +
            "class Bar(object ): \n" +
            "    pass" +
            "";

    doc.set(initial);

    PySelection ps = new PySelection(doc, 0, 20);
    SelectionKeeper keeper = new SelectionKeeper(ps);

    final String finalStr = "" +
            "class Bar(object): \n" +
            "    pass" +
            "";

    doc.set(finalStr);
    final SelectionProvider selectionProvider = new SelectionProvider();
    keeper.restoreSelection(selectionProvider, doc);

    assertEquals(19, selectionProvider.sel.getOffset());
    assertEquals(0, selectionProvider.sel.getLength());
}
 
Example 7
Source File: PySelectionTest.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public void testGetLineOfOffset() throws Exception {
    Document doc = new Document();
    PySelection ps = new PySelection(doc);
    assertEquals(0, ps.getLineOfOffset(-10));
    assertEquals(0, ps.getLineOfOffset(0));
    assertEquals(0, ps.getLineOfOffset(10));

    doc.set("aaa");
    assertEquals(0, ps.getLineOfOffset(-10));
    assertEquals(0, ps.getLineOfOffset(0));
    assertEquals(0, ps.getLineOfOffset(10));

    doc.set("aaa\nbbb");
    assertEquals(0, ps.getLineOfOffset(-10));
    assertEquals(0, ps.getLineOfOffset(0));
    assertEquals(1, ps.getLineOfOffset(10));
}
 
Example 8
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testGettingClass3() throws Exception {
    Document doc = new Document();
    doc.set("class A(object):\n" +
            "    curr_widget_class = 10\n" +
            "\n" +
            "" +
            "");

    List<stmtType> all = FastParser.parseClassesAndFunctions(doc);
    assertEquals(1, all.size());
}
 
Example 9
Source File: LangPartitionScannerTest.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
protected void setupDocument(String docContents) {
	if(recreateDocSetup){ 
		document = new Document(docContents);
		fp = LangDocumentPartitionerSetup.getInstance().setupDocument(document);
	} else {
		document.set(docContents);
		assertNotNull(fp);
	}
}
 
Example 10
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testGettingMethod() throws Exception {
    Document doc = new Document();
    doc.set("def a():\n" +
            "    curr_widget_def = 10\n" +
            "\n" +
            "" +
            "");

    List<stmtType> all = FastParser.parseClassesAndFunctions(doc);
    assertEquals(1, all.size());
}
 
Example 11
Source File: SelectionKeeperTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testSelectionKeeper() throws Exception {
    Document doc = new Document();
    final String initial = "" +
            "aaa\n" +
            "bbb\n" +
            "ccc\n" +
            "ddd\n" +
            "";

    doc.set(initial);

    PySelection ps = new PySelection(doc, 1, 2);
    final int initialOffset = ps.getAbsoluteCursorOffset();
    assertEquals(6, initialOffset);
    SelectionKeeper keeper = new SelectionKeeper(ps);

    doc.set("");

    final SelectionProvider selectionProvider = new SelectionProvider();
    keeper.restoreSelection(selectionProvider, doc);

    assertEquals(0, selectionProvider.sel.getOffset());
    assertEquals(0, selectionProvider.sel.getLength());

    doc.set("aaa\n");
    keeper.restoreSelection(selectionProvider, doc);
    assertEquals(4, selectionProvider.sel.getOffset());
    assertEquals(0, selectionProvider.sel.getLength());

    doc.set("aaa");
    keeper.restoreSelection(selectionProvider, doc);
    assertEquals(2, selectionProvider.sel.getOffset());
    assertEquals(0, selectionProvider.sel.getLength());

    doc.set(initial);
    keeper.restoreSelection(selectionProvider, doc);
    assertEquals(initialOffset, selectionProvider.sel.getOffset());
    assertEquals(0, selectionProvider.sel.getLength());

}
 
Example 12
Source File: PatternExpressionModelBuilder.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private void doBuild(IDocument document, List<ITypedRegion> groovyPartitions) {
    final Set<String> parsedExpressions = Sets.newHashSet();
    expression.getReferencedElements().clear();
    for (final ITypedRegion region : groovyPartitions) {
        try {
            final String expressionContent = strip(document.get(region.getOffset(), region.getLength()));
            if (expressionContent != null && !parsedExpressions.contains(expressionContent)) {
                final Expression groovyScriptExpression = ExpressionHelper
                        .createGroovyScriptExpression(expressionContent, String.class.getName());
                groovyScriptExpression.setName(expressionContent);
                final Document expDoc = new Document();
                expDoc.set(expressionContent);
                final FindReplaceDocumentAdapter expDocFinder = new FindReplaceDocumentAdapter(expDoc);
                for (final Expression exp : scope) {
                    if (expDocFinder.find(0, exp.getName(), true, true, true, false) != null) {
                        groovyScriptExpression.getReferencedElements()
                                .add(ExpressionHelper.createDependencyFromEObject(exp));
                    }
                }
                expression.getReferencedElements().add(groovyScriptExpression);
                parsedExpressions.add(expressionContent);
            }
        } catch (final BadLocationException e) {

        }
    }
}
 
Example 13
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testCython3() throws Exception {
    Document doc = new Document();
    doc.set(""
            + "def a():\n"
            + "    cdef int b\n"
            + "    b2 = 8\n"
            + "    return a\n"
            + "\n"
            + "def a2():\n"
            + "    cdef int b\n"
            + "    b = 6\n"
            + "    return a\n"
            + "\n");
    List<stmtType> stmts = FastParser.parseCython(doc);
    assertEquals(2, stmts.size());
    assertEquals("a", NodeUtils.getRepresentationString(stmts.get(0)));

    FunctionDef st0 = (FunctionDef) stmts.get(0);
    assertEquals(1, st0.body.length);
    assertEquals("int b", NodeUtils.getRepresentationString(st0.body[0]));

    assertEquals("a2", NodeUtils.getRepresentationString(stmts.get(1)));
    FunctionDef st1 = (FunctionDef) stmts.get(1);
    assertEquals(1, st1.body.length);

    String s = printAst(stmts);
    assertEquals(""
            + "def a(self):\n"
            + "    def int b(self):\n"
            + "def a2(self):\n"
            + "    def int b(self):\n"
            + "", s);
}
 
Example 14
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testGettingClass4() throws Exception {
    Document doc = new Document();
    doc.set("\nclass A(object):\n" +
            "    curr_widget_class = 10\n" +
            "\n" +
            "" +
            "");

    List<stmtType> all = FastParser.parseClassesAndFunctions(doc);
    assertEquals(1, all.size());
}
 
Example 15
Source File: PySelectionTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testGetBeforeAndAfterMatchingChars() throws Exception {
    Document doc = new Document();
    PySelection ps = new PySelection(doc);
    assertEquals(new Tuple<String, String>("", ""), ps.getBeforeAndAfterMatchingChars('\''));
    doc.set("''ab");
    assertEquals(new Tuple<String, String>("", "''"), ps.getBeforeAndAfterMatchingChars('\''));
    ps.setSelection(1, 1);
    assertEquals(new Tuple<String, String>("'", "'"), ps.getBeforeAndAfterMatchingChars('\''));
    ps.setSelection(2, 2);
    assertEquals(new Tuple<String, String>("''", ""), ps.getBeforeAndAfterMatchingChars('\''));
    ps.setSelection(3, 3);
    assertEquals(new Tuple<String, String>("", ""), ps.getBeforeAndAfterMatchingChars('\''));
}
 
Example 16
Source File: XtextGMFDocument.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a document containing the full Xtext and diagram contents.
 *
 * @return a new document with Xtext and diagram contents.
 */
@Override
public IDocument createFullContentsDocument() {
  Document result = new Document();
  result.set(this.get() + myResource.getDiagramText(), this.getModificationStamp());
  return result;
}
 
Example 17
Source File: AbstractQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private String evaluateChanges(String uri, List<TextEdit> edits) throws BadLocationException, JavaModelException {
	assertFalse("No edits generated: " + edits, edits == null || edits.isEmpty());
	ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
	assertNotNull("CU not found: " + uri, cu);
	Document doc = new Document();
	if (cu.exists()) {
		doc.set(cu.getSource());
	}
	return TextEditUtil.apply(doc, edits);
}
 
Example 18
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testGettingClass() throws Exception {
    Document doc = new Document();
    doc.set("class Foo:\n" +
            "\n" +
            "class Bar(object):\n" +
            "\n" +
            "    class My\n" +
            "'''class Dont\n"
            +
            "class Dont2\n" +
            "\n" +
            "'''\n" +
            "class My2:\n" +
            "" +
            "");

    List<stmtType> all = FastParser.parseClassesAndFunctions(doc);
    assertEquals(4, all.size());
    check(all, 0, 1, 1, 1, 7);
    check(all, 1, 3, 1, 3, 7);
    check(all, 2, 5, 5, 5, 11);
    check(all, 3, 10, 1, 10, 7);

    stmtType found = FastParser.firstClassOrFunction(doc, 1, true, false);
    checkNode(3, 1, 3, 7, found);

    found = FastParser.firstClassOrFunction(doc, 0, true, false);
    checkNode(1, 1, 1, 7, found);

    found = FastParser.firstClassOrFunction(doc, 5, true, false);
    checkNode(10, 1, 10, 7, found);

    found = FastParser.firstClassOrFunction(doc, 5, false, false);
    checkNode(5, 5, 5, 11, found);

    found = FastParser.firstClassOrFunction(doc, -1, false, false);
    assertNull(found);

    found = FastParser.firstClassOrFunction(doc, 15, true, false);
    assertNull(found);

    found = FastParser.firstClassOrFunction(doc, 15, false, false);
    checkNode(10, 1, 10, 7, found);

}
 
Example 19
Source File: FastParserTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testBackwardsUntil1stGlobal2() throws Exception {
    Document doc = new Document();
    doc.set("def b():\n" +
            "    pass\n" +
            "\n" +
            "def a():\n" +
            "    def f():\n" + //4
            "        curr_widget_def = 10\n" + //5
            "    def c():\n" + //6
            "        curr_widget_def = 10\n" + //7
            "    a = 10\n" + //8
            "    print a" + //9
            "" +
            "");

    List<stmtType> stmts;

    for (int i = 8; i <= 9; i++) {
        //8 and 9
        stmts = FastParser.parseToKnowGloballyAccessiblePath(doc, i);
        assertEquals(1, stmts.size());
        assertEquals("a", NodeUtils.getRepresentationString(stmts.get(0)));
    }

    for (int i = 6; i <= 7; i++) {
        //6 and 7
        stmts = FastParser.parseToKnowGloballyAccessiblePath(doc, i);
        assertEquals(2, stmts.size());
        assertEquals("a", NodeUtils.getRepresentationString(stmts.get(0)));
        assertEquals("c", NodeUtils.getRepresentationString(stmts.get(1)));
    }

    for (int i = 4; i <= 5; i++) {
        //4 and 5
        stmts = FastParser.parseToKnowGloballyAccessiblePath(doc, i);
        assertEquals(2, stmts.size());
        assertEquals("a", NodeUtils.getRepresentationString(stmts.get(0)));
        assertEquals("f", NodeUtils.getRepresentationString(stmts.get(1)));
    }

    //3
    stmts = FastParser.parseToKnowGloballyAccessiblePath(doc, 3);
    assertEquals(1, stmts.size());
    assertEquals("a", NodeUtils.getRepresentationString(stmts.get(0)));

    for (int i = 0; i <= 2; i++) {
        //2, 1 and 0
        stmts = FastParser.parseToKnowGloballyAccessiblePath(doc, i);
        assertEquals(1, stmts.size());
        assertEquals("b", NodeUtils.getRepresentationString(stmts.get(0)));
    }
}
 
Example 20
Source File: LangAutoEditStrategyTest.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
protected Document setupDocument(String contents) {
	Document document = getDocument();
	document.set(contents);
	return document;
}