<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.intellij.openapi.editor.actions;

import com.intellij.codeInsight.editorActions.SelectWordUtil;
import com.intellij.codeInsight.highlighting.BraceMatchingUtil;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SelectWordAtCaretAction extends TextComponentEditorAction implements DumbAware {
  public SelectWordAtCaretAction() {
    super(new DefaultHandler());
    setInjectedContext(true);
  }

  private static final class DefaultHandler extends EditorActionHandler.ForEachCaret {
    @Override
    public void doExecute(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
      assert caret != null;
      Document document = editor.getDocument();

      if (EditorUtil.isPasswordEditor(editor)) {
        caret.setSelection(0, document.getTextLength());
        return;
      }

      int lineNumber = caret.getLogicalPosition().line;
      int caretOffset = caret.getOffset();
      if (lineNumber >= document.getLineCount()) {
        return;
      }

      boolean camel = editor.getSettings().isCamelWords();
      List<TextRange> ranges = new ArrayList<>();

      int textLength = document.getTextLength();
      if (caretOffset == textLength) caretOffset--;
      if (caretOffset < 0) return;

      SelectWordUtil.addWordOrLexemeSelection(camel, editor, caretOffset, ranges);

      // add whole line selection
      int line = document.getLineNumber(caretOffset);
      ranges.add(new TextRange(document.getLineStartOffset(line), document.getLineEndOffset(line)));

      if (ranges.isEmpty()) return;<commit_msg>[lang-impl] SelectWordAtCaretAction: redundant 'if' removed<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.intellij.openapi.editor.actions;

import com.intellij.codeInsight.editorActions.SelectWordUtil;
import com.intellij.codeInsight.highlighting.BraceMatchingUtil;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SelectWordAtCaretAction extends TextComponentEditorAction implements DumbAware {
  public SelectWordAtCaretAction() {
    super(new DefaultHandler());
    setInjectedContext(true);
  }

  private static final class DefaultHandler extends EditorActionHandler.ForEachCaret {
    @Override
    public void doExecute(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
      assert caret != null;
      Document document = editor.getDocument();

      if (EditorUtil.isPasswordEditor(editor)) {
        caret.setSelection(0, document.getTextLength());
        return;
      }

      int lineNumber = caret.getLogicalPosition().line;
      int caretOffset = caret.getOffset();
      if (lineNumber >= document.getLineCount()) {
        return;
      }

      boolean camel = editor.getSettings().isCamelWords();
      List<TextRange> ranges = new ArrayList<>();

      int textLength = document.getTextLength();
      if (caretOffset == textLength) caretOffset--;
      if (caretOffset < 0) return;

      SelectWordUtil.addWordOrLexemeSelection(camel, editor, caretOffset, ranges);

      // add whole line selection
      int line = document.getLineNumber(caretOffset);
      ranges.add(new TextRange(document.getLineStartOffset(line), document.getLineEndOffset(line)));
{'fraction_non_alphanumeric': 0.09818895919703251, 'fraction_numerical': 0.005236744490508401, 'mean_word_length': 4.450653983353151, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3025154', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1309, 'n_words': 286}