protected void addExpressionStyles(int lineOffset, int lineLenght, Vector<StyleRange> styles) {
   final String content = document.get();
   for (Expression exp : expressions) {
     if (supportedTypes.keySet().contains(exp.getType())) {
       try {
         int i = lineOffset;
         IRegion index = null;
         index = finder.find(i, exp.getName(), true, true, true, false);
         while (index != null && index.getOffset() < lineOffset + lineLenght) {
           if (PatternLineStyleListener.isNotEscapeWord(content, index.getOffset())) {
             styles.add(
                 new StyleRange(
                     index.getOffset(),
                     index.getLength(),
                     Display.getDefault().getSystemColor(supportedTypes.get(exp.getType())),
                     null,
                     SWT.BOLD));
           }
           i = index.getOffset() + index.getLength();
           if (i < lineOffset + lineLenght) {
             index = finder.find(i, exp.getName(), true, true, true, false);
           } else {
             index = null;
           }
         }
       } catch (BadLocationException e) {
         // Ignore
       }
     }
   }
 }
  private static void loadRegions(List<TestRegion> regionList, IDocument document)
      throws BadLocationException {
    FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(document);
    // IRegion region = adapter.find(0, "{", true, true, false, false);
    // if(region == null)
    IRegion region = new Region(0, 0);
    for (TestRegion testRegion : regionList) {
      IRegion newRegion =
          adapter.find(
              region.getOffset() + region.getLength(),
              testRegion.regionText,
              true,
              true,
              false,
              false);
      if (newRegion != null) {
        testRegion.region = newRegion;
        region = newRegion;
      } else fail("Can not find string - " + testRegion.regionText);
    }

    for (int i = regionList.size() - 1; i >= 0; i--) {
      TestRegion r = regionList.get(i);
      if (r.hyperlinks.size() == 0) regionList.remove(r);
    }
  }
  public boolean openHyperLink() {
    String localiz = getLocalization();
    if (localiz == null) {
      return false;
    } else if (fBase.getUnderlyingResource() == null) {
      return false;
    } else if (fElement.length() == 0 || fElement.charAt(0) != '%') {
      return false;
    }

    IProject proj = fBase.getUnderlyingResource().getProject();
    IFile file = proj.getFile(localiz + ".properties"); // $NON-NLS-1$
    if (!file.exists()) return false;

    try {
      IEditorPart editor = IDE.openEditor(PDEPlugin.getActivePage(), file);
      if (!(editor instanceof TextEditor)) return false;
      TextEditor tEditor = (TextEditor) editor;
      IDocument doc = tEditor.getDocumentProvider().getDocument(tEditor.getEditorInput());
      if (doc == null) return false;

      try {
        String key = fElement.substring(1);
        int keyLen = key.length();
        int length = doc.getLength();
        int start = 0;
        IRegion region = null;
        FindReplaceDocumentAdapter docSearch = new FindReplaceDocumentAdapter(doc);
        while ((region = docSearch.find(start, key, true, false, false, false)) != null) {
          int offset = region.getOffset();
          if (offset > 0) {
            // check for newline before
            char c = doc.getChar(offset - 1);
            if (c != '\n' && c != '\r') {
              start += keyLen;
              continue;
            }
          }
          if (offset + keyLen < length) {
            // check for whitespace / assign symbol after
            char c = doc.getChar(offset + keyLen);
            if (!Character.isWhitespace(c) && c != '=' && c != ':') {
              start += keyLen;
              continue;
            }
          }
          tEditor.selectAndReveal(offset, keyLen);
          break;
        }
      } catch (BadLocationException e) {
        PDEPlugin.log(e);
      }

    } catch (PartInitException e) {
      return false;
    }
    return true;
  }
Esempio n. 4
0
  private boolean initializePatternControl() {
    ISelection selection = getSelection();
    if (selection instanceof ITextSelection
        && !selection.isEmpty()
        && ((ITextSelection) selection).getLength() > 0) {
      String text = ((ITextSelection) selection).getText();
      if (text != null) {
        if (fIsRegExSearch) {
          fPattern.setText(FindReplaceDocumentAdapter.escapeForRegExPattern(text));
        } else {
          fPattern.setText(insertEscapeChars(text));
        }

        if (fPreviousExtensions.length > 0) {
          fExtensions.setText(fPreviousExtensions[0]);
        } else {
          String extension = getExtensionFromEditor();
          if (extension != null) {
            fExtensions.setText(extension);
          } else {
            fExtensions.setText("*"); // $NON-NLS-1$
          }
        }
        return true;
      }
    }
    return false;
  }
Esempio n. 5
0
  @Override
  protected void apply(IDocument document, IStructuredModel model, Node node, int start, int end) {
    String message = mMarker.getAttribute(IMarker.MESSAGE, "");
    String typo = TypoDetector.getTypo(message);
    if (typo == null) {
      return;
    }
    List<String> replacements = TypoDetector.getSuggestions(message);
    if (replacements.size() == 0) {
      return;
    }

    try {
      String current = document.get(start, end - start);
      if (current.equals(typo)) {
        document.replace(start, end - start, replacements.get(0));
      } else {
        // The buffer has been edited; try to find the typo.
        FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(document);
        IRegion forward = finder.find(start, typo, true /*forward*/, true, true, false);
        IRegion backward = finder.find(start, typo, false /*forward*/, true, true, false);
        if (forward != null && backward != null) {
          // Pick the closest one
          int forwardDelta = forward.getOffset() - start;
          int backwardDelta = start - backward.getOffset();
          if (forwardDelta < backwardDelta) {
            start = forward.getOffset();
          } else {
            start = backward.getOffset();
          }
        } else if (forward != null) {
          start = forward.getOffset();
        } else if (backward != null) {
          start = backward.getOffset();
        } else {
          return;
        }
        end = start + typo.length();
        document.replace(start, end - start, replacements.get(0));
      }
    } catch (BadLocationException e) {
      AdtPlugin.log(e, null);
    }
  }
Esempio n. 6
0
  private int findChangeLogEntry(IDocument changelog_doc, String entry) {
    FindReplaceDocumentAdapter findDocumentAptd = new FindReplaceDocumentAdapter(changelog_doc);
    IRegion region = null;
    try {
      region = findDocumentAptd.find(0, entry, true, false, /*whole world */ false, true);
    } catch (BadLocationException e) {
      ChangelogPlugin.getDefault()
          .getLog()
          .log(
              new Status(
                  IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));

      return -1;
    }
    if (region != null) {
      // If the user's entry is not at the beginning of the file,
      // make a new entry.
      return region.getOffset() > 0 ? -1 : 0;
    } else return -1;
  }
 public List<IRegion> searchOccurrences(String searchFor) {
   ArrayList<IRegion> lst = new ArrayList<IRegion>();
   FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(this.doc);
   boolean regExSearch = false;
   boolean wholeWord = true;
   boolean caseSensitive = true;
   boolean forwardSearch = true;
   int startOffset = 0;
   try {
     while (true) {
       IRegion found =
           adapter.find(
               startOffset, searchFor, forwardSearch, caseSensitive, wholeWord, regExSearch);
       if (found == null) {
         break;
       }
       lst.add(found);
       startOffset = found.getOffset() + found.getLength();
     }
   } catch (BadLocationException e) {
     Log.log(e);
   }
   return lst;
 }
Esempio n. 8
0
 public void run() {
   ISelection selection =
       PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
   if (selection instanceof StructuredSelection) {
     StructuredSelection structuredSelection = (StructuredSelection) selection;
     if (structuredSelection.size() == 1) {
       if (structuredSelection.getFirstElement() instanceof IFile) {
         IFile implementationNote = (IFile) structuredSelection.getFirstElement();
         if (implementationNote.getFileExtension().equals("int")) {
           InputStream inStream;
           try {
             inStream = implementationNote.getContents();
             long byteCount = implementationNote.getRawLocation().toFile().length();
             byte[] byteArray = new byte[(int) byteCount];
             inStream.read(byteArray);
             IDocument document = new Document(new String(byteArray));
             FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
             IRegion startRegion = frda.find(0, "8. Code Changes", true, true, true, false);
             int startOffset = startRegion.getOffset() + 17;
             IRegion endRegion = frda.find(startOffset, "End", true, true, true, false);
             int endOffset = endRegion.getOffset();
             String fileList = document.get(startOffset, endOffset - startOffset);
             fileList = fileList.replaceAll("---------------\r\n", "");
             fileList = fileList.replaceAll("Branch name:.*\r\n", "");
             fileList = fileList.replaceAll("\r\n    ", "");
             fileList = fileList.replaceAll("\r\n", ",");
             fileList = fileList.replaceAll(">", "");
             String[] filesString = fileList.split(",");
             if (filesString.length == 0) return;
             ArrayList<IFile> list = new ArrayList<IFile>();
             for (int i = 0; i < filesString.length; i++) {
               if (!filesString[i].equals("")) {
                 IResource resource =
                     ResourcesPlugin.getWorkspace().getRoot().findMember(filesString[i]);
                 if (resource instanceof IFile) {
                   list.add((IFile) resource);
                 }
               }
             }
             if (list.size() == 0) return;
             IWorkingSetManager workingSetManager =
                 PlatformUI.getWorkbench().getWorkingSetManager();
             String workingSetName =
                 "chgset-" + implementationNote.getName().replaceAll(".int", "");
             IWorkingSet set =
                 workingSetManager.createWorkingSet(
                     workingSetName, list.toArray(new IFile[list.size()]));
             IWorkingSet existingSet = workingSetManager.getWorkingSet(workingSetName);
             if (existingSet != null) {
               workingSetManager.removeWorkingSet(existingSet);
             }
             workingSetManager.addWorkingSet(set);
             inStream.close();
           } catch (CoreException e) {
           } catch (IOException e) {
           } catch (BadLocationException e) {
           }
         }
       }
     }
   }
 }