Java Code Examples for com.intellij.find.FindManager#getInstance()

The following examples show how to use com.intellij.find.FindManager#getInstance() . 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: FindInProjectManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @param model would be used for search if not null, otherwise shared (project-level) model would be used
 */
public void findInProject(@Nonnull DataContext dataContext, @Nullable FindModel model) {

  final FindManager findManager = FindManager.getInstance(myProject);
  final FindModel findModel;
  if (model != null) {
    findModel = model.clone();
  }
  else {
    findModel = findManager.getFindInProjectModel().clone();
    findModel.setReplaceState(false);
    initModel(findModel, dataContext);
  }

  findManager.showFindDialog(findModel, () -> {
    if (findModel.isReplaceState()) {
      ReplaceInProjectManager.getInstance(myProject).replaceInPath(findModel);
    }
    else {
      findInPath(findModel);
    }
  });
}
 
Example 2
Source File: HighlightHandlerBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void setupFindModel(final Project project) {
  final FindManager findManager = FindManager.getInstance(project);
  FindModel model = findManager.getFindNextModel();
  if (model == null) {
    model = findManager.getFindInFileModel();
  }
  model.setSearchHighlighters(true);
  findManager.setFindWasPerformed();
  findManager.setFindNextModel(model);
}
 
Example 3
Source File: EscapeHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext){
  editor.setHeaderComponent(null);

  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project != null) {
    HighlightManagerImpl highlightManager = (HighlightManagerImpl)HighlightManager.getInstance(project);
    if (highlightManager != null && highlightManager.hideHighlights(editor, HighlightManager.HIDE_BY_ESCAPE | HighlightManager.HIDE_BY_ANY_KEY)) {

      StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
      if (statusBar != null) {
        statusBar.setInfo("");
      }

      FindManager findManager = FindManager.getInstance(project);
      if (findManager != null) {
        FindModel model = findManager.getFindNextModel(editor);
        if (model != null) {
          model.setSearchHighlighters(false);
          findManager.setFindNextModel(model);
        }
      }

      return;
    }
  }

  myOriginalHandler.execute(editor, dataContext);
}
 
Example 4
Source File: IncrementalFindAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Editor editor, DataContext dataContext) {
  final Project project = DataManager.getInstance().getDataContext(editor.getComponent()).getData(CommonDataKeys.PROJECT);
  if (!editor.isOneLineMode()) {
    EditorSearchSession search = EditorSearchSession.get(editor);
    if (search != null) {
      search.getComponent().requestFocusInTheSearchFieldAndSelectContent(project);
      FindUtil.configureFindModel(myReplace, editor, search.getFindModel(), false);
    } else {
      FindManager findManager = FindManager.getInstance(project);
      FindModel model;
      if (myReplace) {
        model = findManager.createReplaceInFileModel();
      } else {
        model = new FindModel();
        model.copyFrom(findManager.getFindInFileModel());
      }
      boolean consoleViewEditor = ConsoleViewUtil.isConsoleViewEditor(editor);
      FindUtil.configureFindModel(myReplace, editor, model, consoleViewEditor);
      EditorSearchSession.start(editor, model, project).getComponent()
              .requestFocusInTheSearchFieldAndSelectContent(project);
      if (!consoleViewEditor && editor.getSelectionModel().hasSelection()) {
        // selection is used as string to find without search model modification so save the pattern explicitly
        FindUtil.updateFindInFileModel(project, model, true);
      }
    }
  }
}
 
Example 5
Source File: SelectAllOccurrencesAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) {
  Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c;

  if (!caret.hasSelection()) {
    TextRange wordSelectionRange = getSelectionRange(editor, caret);
    if (wordSelectionRange != null) {
      setSelection(editor, caret, wordSelectionRange);
    }
  }

  String selectedText = caret.getSelectedText();
  Project project = editor.getProject();
  if (project == null || selectedText == null) {
    return;
  }

  int caretShiftFromSelectionStart = caret.getOffset() - caret.getSelectionStart();
  FindManager findManager = FindManager.getInstance(project);

  FindModel model = new FindModel();
  model.setStringToFind(selectedText);
  model.setCaseSensitive(true);
  model.setWholeWordsOnly(true);

  int searchStartOffset = 0;
  FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model);
  while (findResult.isStringFound()) {
    int newCaretOffset = caretShiftFromSelectionStart + findResult.getStartOffset();
    EditorActionUtil.makePositionVisible(editor, newCaretOffset);
    Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(newCaretOffset));
    if (newCaret != null) {
      setSelection(editor, newCaret, findResult);
    }
    findResult = findManager.findString(editor.getDocument().getCharsSequence(), findResult.getEndOffset(), model);
  }
  editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
 
Example 6
Source File: FindInProjectManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void startFindInProject(@Nonnull FindModel findModel) {
  if (findModel.getDirectoryName() != null && FindInProjectUtil.getDirectory(findModel) == null) {
    return;
  }

  UsageViewManager manager = UsageViewManager.getInstance(myProject);

  if (manager == null) return;
  final FindManager findManager = FindManager.getInstance(myProject);
  findManager.getFindInProjectModel().copyFrom(findModel);
  final FindModel findModelCopy = findModel.clone();
  final UsageViewPresentation presentation = FindInProjectUtil.setupViewPresentation(findModelCopy);
  final FindUsagesProcessPresentation processPresentation = FindInProjectUtil.setupProcessPresentation(myProject, presentation);
  ConfigurableUsageTarget usageTarget = new FindInProjectUtil.StringUsageTarget(myProject, findModel);

  ((FindManagerImpl)FindManager.getInstance(myProject)).getFindUsagesManager().addToHistory(usageTarget);

  manager.searchAndShowUsages(new UsageTarget[]{usageTarget}, () -> processor -> {
    myIsFindInProgress = true;

    try {
      Processor<UsageInfo> consumer = info -> {
        Usage usage = UsageInfo2UsageAdapter.CONVERTER.fun(info);
        usage.getPresentation().getIcon(); // cache icon
        return processor.process(usage);
      };
      FindInProjectUtil.findUsages(findModelCopy, myProject, consumer, processPresentation);
    }
    finally {
      myIsFindInProgress = false;
    }
  }, processPresentation, presentation, null);
}
 
Example 7
Source File: SearchResults.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void findInRange(@Nonnull TextRange range, @Nonnull Editor editor, @Nonnull FindModel findModel, @Nonnull List<? super FindResult> results) {
  VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(editor.getDocument());

  // Document can change even while we're holding read lock (example case - console), so we're taking an immutable snapshot of text here
  CharSequence charSequence = editor.getDocument().getImmutableCharSequence();

  int offset = range.getStartOffset();
  int maxOffset = Math.min(range.getEndOffset(), charSequence.length());
  FindManager findManager = FindManager.getInstance(getProject());

  while (true) {
    FindResult result;
    try {
      CharSequence bombedCharSequence = StringUtil.newBombedCharSequence(charSequence, 3000);
      result = findManager.findString(bombedCharSequence, offset, findModel, virtualFile);
      ((StringUtil.BombedCharSequence)bombedCharSequence).defuse();
    }
    catch (PatternSyntaxException | ProcessCanceledException e) {
      result = null;
    }
    if (result == null || !result.isStringFound()) break;
    final int newOffset = result.getEndOffset();
    if (result.getEndOffset() > maxOffset) break;
    if (offset == newOffset) {
      if (offset < maxOffset - 1) {
        offset++;
      }
      else {
        results.add(result);
        break;
      }
    }
    else {
      offset = newOffset;
      if (offset == result.getStartOffset()) ++offset; // skip zero width result
    }
    results.add(result);
  }
}
 
Example 8
Source File: HighlightSymbolAction.java    From emacsIDEAs with Apache License 2.0 4 votes vote down vote up
private int getNextSymbolOffset(boolean searchForward, Project project) {
    _editor.getSelectionModel().selectWordAtCaret(false);

    int symbolStart = _editor.getSelectionModel().getSelectionStart();
    int symbolEnd = _editor.getSelectionModel().getSelectionEnd();

    if (symbolStart >= 0 && symbolEnd > symbolStart) {
        String symbol =  _editor.getDocument().getText(new TextRange(symbolStart, symbolEnd));

        FindManager findManager = FindManager.getInstance(project);
        FindModel findModel = (FindModel) findManager.getFindInFileModel().clone();
        findModel.setFindAll(false);
        findModel.setFromCursor(true);
        findModel.setForward(searchForward);
        findModel.setRegularExpressions(false);
        findModel.setWholeWordsOnly(true);
        findModel.setCaseSensitive(true);
        findModel.setSearchHighlighters(false);
        findModel.setPreserveCase(false);

        findModel.setStringToFind(symbol);

        int startOffset = _editor.getCaretModel().getOffset();
        if (searchForward) {
            startOffset++;
        }

        FindResult findResult = findManager.findString(_editor.getDocument().getText(), startOffset, findModel);

        //fix errors in Appcode, which is the findManager.findString return 0, when string not found.
        if (findResult.getStartOffset() == 0) {
            String potentialSymbol =  _editor.getDocument().getText(new TextRange(0, symbol.length()));
            if (!potentialSymbol.equals(symbol)) {
                return -1;
            }
        }

        return findResult.getStartOffset();
    }

    return -1;
}
 
Example 9
Source File: SelectNextOccurrenceAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) {
  Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c;
  TextRange wordSelectionRange = getSelectionRange(editor, caret);
  boolean notFoundPreviously = getAndResetNotFoundStatus(editor);
  boolean wholeWordSearch = isWholeWordSearch(editor);
  if (caret.hasSelection()) {
    Project project = editor.getProject();
    String selectedText = caret.getSelectedText();
    if (project == null || selectedText == null) {
      return;
    }
    FindManager findManager = FindManager.getInstance(project);

    FindModel model = new FindModel();
    model.setStringToFind(selectedText);
    model.setCaseSensitive(true);
    model.setWholeWordsOnly(wholeWordSearch);

    int searchStartOffset = notFoundPreviously ? 0 : caret.getSelectionEnd();
    FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model);
    if (findResult.isStringFound()) {
      int newCaretOffset = caret.getOffset() - caret.getSelectionStart() + findResult.getStartOffset();
      EditorActionUtil.makePositionVisible(editor, newCaretOffset);
      Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(newCaretOffset));
      if (newCaret == null) {
        // this means that the found occurence is already selected
        if (notFoundPreviously) {
          setNotFoundStatus(editor); // to make sure we won't show hint anymore if there are no more occurrences
        }
      }
      else {
        setSelection(editor, newCaret, findResult);
      }
    }
    else {
      setNotFoundStatus(editor);
      showHint(editor);
    }
  }
  else {
    if (wordSelectionRange == null) {
      return;
    }
    setSelection(editor, caret, wordSelectionRange);
    setWholeWordSearch(editor, true);
  }
  editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}