private Collection<Token> performTest(String sourceCode, final int offset, boolean[] wasResolved)
      throws Exception {
    FileObject root = makeScratchDir(this);

    FileObject sourceDir = root.createFolder("src");
    FileObject buildDir = root.createFolder("build");
    FileObject cacheDir = root.createFolder("cache");

    source = sourceDir.createFolder("test").createData("Test.java");

    writeIntoFile(source, sourceCode);

    SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]);

    DataObject od = DataObject.find(source);
    EditorCookie ec = od.getCookie(EditorCookie.class);
    Document doc = ec.openDocument();

    doc.putProperty(Language.class, JavaTokenId.language());
    doc.putProperty("mimeType", "text/x-java");

    return InstantRenamePerformer.computeChangePoints(
        SourceUtilsTestUtil.getCompilationInfo(JavaSource.forFileObject(source), Phase.RESOLVED),
        offset,
        wasResolved);
  }
예제 #2
0
  final void setPage(HtmlPage page) throws IOException {

    this.page = page;
    final Document doc = textPane.getDocument();
    doc.putProperty(Document.TitleProperty, page.getTitle());
    // Clearing stream forces refresh.
    doc.putProperty(Document.StreamDescriptionProperty, null);
    textPane.setPage(page.getPath().toURI().toURL());
    find(find.getText());
  }
예제 #3
0
  public void setPage(final URL page) throws IOException {
    if (page == null) {
      throw new IOException(Messages.getString("swing.03", "Page")); // $NON-NLS-1$ //$NON-NLS-2$
    }

    String url = page.toString();
    String baseUrl = getBaseURL(url);
    Document oldDoc = getDocument();
    if (baseUrl != null
        && oldDoc != null
        && baseUrl.equals(oldDoc.getProperty(Document.StreamDescriptionProperty))) {

      scrollToReference(page.getRef());
      return;
    }
    InputStream stream = getStream(page);
    if (stream == null) {
      return;
    }
    Document newDoc = editorKit.createDefaultDocument();
    // Perhaps, it is reasonable only for HTMLDocument...
    if (newDoc instanceof HTMLDocument) {
      newDoc.putProperty(Document.StreamDescriptionProperty, baseUrl);
      newDoc.putProperty(StringConstants.IGNORE_CHARSET_DIRECTIVE, new Boolean(false));
      try {
        ((HTMLDocument) newDoc).setBase(new URL(baseUrl));
      } catch (IOException e) {
      }
    }
    // TODO Asynch loading doesn't work with completely.
    // Also page property change event is written incorrectly now
    // (at the asynchrounous loading), because loading may not be
    // completed.
    // int asynchronousLoadPriority = getAsynchronousLoadPriority(newDoc);
    int asynchronousLoadPriority = -1;
    if (asynchronousLoadPriority >= 0) {
      setDocument(newDoc);
      AsynchLoad newThread = new AsynchLoad(asynchronousLoadPriority, stream, page);
      newThread.start();
      if (newThread.successfulLoading) {
        changePage(page);
      }
    } else {
      try {
        documentLoading(stream, newDoc, page);
        stream.close();
        setDocument(newDoc);
        changePage(page);
      } catch (IOException e) {
      }
    }
  }
  @Override
  public void actionPerformed(PhpModule phpModule) {
    // Check CakePHP project
    if (!CakePhpUtils.isCakePHP(phpModule)) {
      return;
    }

    // get EditorCookie
    Lookup lookup = Utilities.actionsGlobalContext();
    EditorCookie context = lookup.lookup(EditorCookie.class);
    if (context == null) {
      return;
    }

    Document doc = context.getDocument();
    if (doc == null) {
      return;
    }

    reformat(doc);

    // Change line feed
    if (!BaseDocument.LS_LF.equals(doc.getProperty(BaseDocument.READ_LINE_SEPARATOR_PROP))) {
      doc.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, BaseDocument.LS_LF);
    }
  }
 private static OffsetsBag getBag(Document doc) {
   OffsetsBag bag = (OffsetsBag) doc.getProperty(RemoveSurroundingCodePanel.class);
   if (bag == null) {
     doc.putProperty(RemoveSurroundingCodePanel.class, bag = new OffsetsBag(doc));
   }
   return bag;
 }
예제 #6
0
 private static TokenSequence<SQLTokenId> getTokenSequence(String sql)
     throws BadLocationException {
   Document doc = new ModificationTextDocument();
   doc.insertString(0, sql, null);
   doc.putProperty(Language.class, SQLTokenId.language());
   TokenHierarchy<?> hi = TokenHierarchy.get(doc);
   TokenSequence<SQLTokenId> seq = hi.tokenSequence(SQLTokenId.language());
   seq.moveStart();
   return seq;
 }
예제 #7
0
 public void read(final InputStream stream, final Object type) throws IOException {
   if (type instanceof String) {
     setContentType((String) type);
   }
   try {
     Document doc = getDocument();
     doc.putProperty(StringConstants.IGNORE_CHARSET_DIRECTIVE, Boolean.TRUE);
     editorKit.read(new InputStreamReader(stream), doc, 0);
   } catch (BadLocationException e) {
   }
 }
예제 #8
0
 private void documentLoading(final InputStream str, final Document doc, final URL url)
     throws IOException {
   try {
     editorKit.read(str, doc, 0);
   } catch (ChangedCharSetException e) {
     try {
       doc.putProperty(StringConstants.IGNORE_CHARSET_DIRECTIVE, Boolean.TRUE);
       doc.remove(0, doc.getLength());
       final String htmlAttribute = e.getCharSetSpec();
       final int charSetIndex = htmlAttribute.lastIndexOf("charset=");
       if (charSetIndex >= 0) {
         String charSet = htmlAttribute.substring(charSetIndex + 8);
         InputStreamReader reader =
             new InputStreamReader(url.openStream(), Charset.forName(charSet));
         editorKit.read(reader, doc, 0);
       }
     } catch (BadLocationException e1) {
     }
   } catch (BadLocationException e) {
   }
 }
예제 #9
0
 public static void refresh() throws IOException {
   log_out.flush();
   Document doc = messOut.getDocument();
   doc.putProperty(Document.StreamDescriptionProperty, null);
   messOut.setPage(logfileURL);
 }
  /** @throws java.lang.Exception */
  @RandomlyFails
  public void testDocumentModification() throws Exception {

    // 1) register tasks and parsers
    MockServices.setServices(MockMimeLookup.class, MyScheduler.class);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    final CountDownLatch latch3 = new CountDownLatch(3);
    final int[] fooParser = {1};
    final int[] fooParserResult = {1};
    final int[] fooEmbeddingProvider = {1};
    final int[] fooTask = {1};
    final int[] booParser = {1};
    final int[] booParserResult = {1};
    final int[] booTask = {1};
    final TestComparator test =
        new TestComparator(
            "1 - reschedule all schedulers\n"
                + "foo get embeddings 1 (Snapshot 1), \n"
                + "Snapshot 1: Toto je testovaci file, na kterem se budou delat hnusne pokusy!!!, \n"
                + "Snapshot 2: stovaci fi, \n"
                + "foo parse 1 (Snapshot 1, FooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + "foo get result 1 (FooParserResultTask 1), \n"
                + "foo task 1 (FooResult 1 (Snapshot 1), SchedulerEvent 1), \n"
                + "foo invalidate 1, \n"
                + "boo parse 1 (Snapshot 2, BooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + "boo get result 1 (BooParserResultTask 1), \n"
                + "boo task 1 (BooResult 1 (Snapshot 2), SchedulerEvent 1), \n"
                + "boo invalidate 1, \n"
                + "2 - insert 14 chars on offset 22\n"
                + "foo get embeddings 1 (Snapshot 3), \n"
                + "Snapshot 3: Toto je testovaci file (druha verze), na kterem se budou delat hnusne pokusy!!!, \n"
                + "Snapshot 4: stovaci fi, \n"
                + "foo parse 1 (Snapshot 3, FooParserResultTask 1, SourceModificationEvent 18:37), \n"
                + "foo get result 1 (FooParserResultTask 1), \n"
                + "foo task 1 (FooResult 2 (Snapshot 3), SchedulerEvent 1), \n"
                + "foo invalidate 2, \n"
                + "boo parse 1 (Snapshot 4, BooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + // !! source unchanged
                "boo get result 1 (BooParserResultTask 1), \n"
                + "boo task 1 (BooResult 2 (Snapshot 4), SchedulerEvent 1), \n"
                + "boo invalidate 2, \n"
                + "3 - remove 5 chars on offset 44\n"
                + "foo get embeddings 1 (Snapshot 5), \n"
                + "Snapshot 5: Toto je testovaci file (druha verze), na ktee budou delat hnusne pokusy!!!, \n"
                + "Snapshot 6: stovaci fi, \n"
                + "foo parse 1 (Snapshot 5, FooParserResultTask 1, SourceModificationEvent 41:45), \n"
                + "foo get result 1 (FooParserResultTask 1), \n"
                + "foo task 1 (FooResult 3 (Snapshot 5), SchedulerEvent 2), \n"
                + "foo invalidate 3, \n"
                + "boo parse 1 (Snapshot 6, BooParserResultTask 1, SourceModificationEvent -1:-1), \n"
                + // !! source unchanged
                "boo get result 1 (BooParserResultTask 1), \n"
                + "boo task 1 (BooResult 3 (Snapshot 6), SchedulerEvent 2), \n"
                + "boo invalidate 3, \n"
                + "4 - end\n");

    MockMimeLookup.setInstances(
        MimePath.get("text/foo"),
        new ParserFactory() {
          public Parser createParser(Collection<Snapshot> snapshots2) {
            return new Parser() {

              private Snapshot last;
              private int i = fooParser[0]++;

              public void parse(Snapshot snapshot, Task task, SourceModificationEvent event)
                  throws ParseException {
                test.check(
                    "foo parse "
                        + i
                        + " (Snapshot "
                        + test.get(snapshot)
                        + ", "
                        + task
                        + ", "
                        + event
                        + "), \n");
                last = snapshot;
              }

              public Result getResult(Task task) throws ParseException {
                test.check("foo get result " + i + " (" + task + "), \n");
                return new Result(last) {

                  public void invalidate() {
                    test.check("foo invalidate " + i + ", \n");
                  }

                  private int i = fooParserResult[0]++;

                  @Override
                  public String toString() {
                    return "FooResult " + i + " (Snapshot " + test.get(getSnapshot()) + ")";
                  }
                };
              }

              public void cancel() {}

              public void addChangeListener(ChangeListener changeListener) {}

              public void removeChangeListener(ChangeListener changeListener) {}
            };
          }
        },
        new TaskFactory() {
          public Collection<SchedulerTask> create(Snapshot snapshot) {
            return Arrays.asList(
                new SchedulerTask[] {
                  new EmbeddingProvider() {

                    private int i = fooEmbeddingProvider[0]++;

                    public List<Embedding> getEmbeddings(Snapshot snapshot) {
                      test.check(
                          "foo get embeddings " + i + " (Snapshot " + test.get(snapshot) + "), \n");
                      test.check(
                          "Snapshot " + test.get(snapshot) + ": " + snapshot.getText() + ", \n");
                      Embedding embedding = snapshot.create(10, 10, "text/boo");
                      test.get(embedding.getSnapshot());
                      test.check(
                          "Snapshot "
                              + test.get(embedding.getSnapshot())
                              + ": "
                              + embedding.getSnapshot().getText()
                              + ", \n");
                      return Arrays.asList(new Embedding[] {embedding});
                    }

                    public int getPriority() {
                      return 10;
                    }

                    public void cancel() {}
                  },
                  new ParserResultTask() {

                    public void run(Result result, SchedulerEvent event) {
                      test.check(
                          "foo task "
                              + i
                              + " ("
                              + result
                              + ", SchedulerEvent "
                              + test.get(event)
                              + "), \n");
                    }

                    public int getPriority() {
                      return 100;
                    }

                    public Class<? extends Scheduler> getSchedulerClass() {
                      return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER;
                    }

                    public void cancel() {}

                    private int i = fooTask[0]++;

                    @Override
                    public String toString() {
                      return "FooParserResultTask " + i;
                    }
                  }
                });
          }
        });
    MockMimeLookup.setInstances(
        MimePath.get("text/boo"),
        new ParserFactory() {
          public Parser createParser(Collection<Snapshot> snapshots2) {
            return new Parser() {

              private Snapshot last;
              private int i = booParser[0]++;

              public void parse(Snapshot snapshot, Task task, SourceModificationEvent event)
                  throws ParseException {
                test.check(
                    "boo parse "
                        + i
                        + " (Snapshot "
                        + test.get(snapshot)
                        + ", "
                        + task
                        + ", "
                        + event
                        + "), \n");
                last = snapshot;
              }

              public Result getResult(Task task) throws ParseException {
                test.check("boo get result " + i + " (" + task + "), \n");
                return new Result(last) {
                  public void invalidate() {
                    test.check("boo invalidate " + i + ", \n");
                    latch1.countDown();
                    latch2.countDown();
                    latch3.countDown();
                  }

                  private int i = booParserResult[0]++;

                  @Override
                  public String toString() {
                    return "BooResult " + i + " (Snapshot " + test.get(getSnapshot()) + ")";
                  }
                };
              }

              public void cancel() {}

              public void addChangeListener(ChangeListener changeListener) {}

              public void removeChangeListener(ChangeListener changeListener) {}
            };
          }
        },
        new TaskFactory() {
          public Collection<SchedulerTask> create(Snapshot snapshot) {
            return Arrays.asList(
                new SchedulerTask[] {
                  new ParserResultTask() {

                    private int i = booTask[0]++;

                    public void run(Result result, SchedulerEvent event) {
                      test.check(
                          "boo task "
                              + i
                              + " ("
                              + result
                              + ", SchedulerEvent "
                              + test.get(event)
                              + "), \n");
                    }

                    public int getPriority() {
                      return 150;
                    }

                    public Class<? extends Scheduler> getSchedulerClass() {
                      return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER;
                    }

                    public void cancel() {}

                    @Override
                    public String toString() {
                      return "BooParserResultTask " + i;
                    }
                  }
                });
          }
        });

    // 2) create source file
    clearWorkDir();
    FileObject workDir = FileUtil.toFileObject(getWorkDir());
    FileObject testFile = FileUtil.createData(workDir, "bla.foo");
    FileUtil.setMIMEType("foo", "text/foo");
    OutputStream outputStream = testFile.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(outputStream);
    writer.append("Toto je testovaci file, na kterem se budou delat hnusne pokusy!!!");
    writer.close();
    Source source = Source.create(testFile);
    Document document = source.getDocument(true);
    document.putProperty("mimeType", "text/foo");
    document.putProperty(Language.class, new ALanguageHierarchy().language());
    TokenHierarchy th = TokenHierarchy.get(document);
    TokenSequence ts = th.tokenSequence();
    ts.tokenCount();
    test.check("1 - reschedule all schedulers\n");

    // 3) shcedulle CurrentDocumentScheduler
    for (Scheduler scheduler : Schedulers.getSchedulers())
      if (scheduler instanceof CurrentDocumentScheduler)
        ((CurrentDocumentScheduler) scheduler).schedule(source);
    latch1.await();
    test.check("2 - insert 14 chars on offset 22\n");

    document.insertString(22, " (druha verze)", null);
    latch2.await();
    test.check("3 - remove 5 chars on offset 44\n");

    document.remove(44, 5);
    latch3.await();
    test.check("4 - end\n");

    assertEquals("", test.getResult());
  }