update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / editorActions / wordSelection / StatementGroupSelectioner.java
blobea2c55811b2a54f6548917e46952d727bf1da080
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.editorActions.wordSelection;
18 import com.intellij.psi.*;
19 import com.intellij.psi.impl.source.jsp.jspJava.JspCodeBlock;
20 import com.intellij.psi.javadoc.PsiDocComment;
21 import com.intellij.openapi.util.TextRange;
22 import com.intellij.openapi.util.text.LineTokenizer;
23 import com.intellij.openapi.editor.Editor;
25 import java.util.List;
26 import java.util.ArrayList;
28 public class StatementGroupSelectioner extends BasicSelectioner {
29 public boolean canSelect(PsiElement e) {
30 return e instanceof PsiStatement || e instanceof PsiComment && !(e instanceof PsiDocComment);
33 public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
34 List<TextRange> result = new ArrayList<TextRange>();
36 PsiElement parent = e.getParent();
38 if (!(parent instanceof PsiCodeBlock) && !(parent instanceof PsiBlockStatement) || parent instanceof JspCodeBlock) {
39 return result;
43 PsiElement startElement = e;
44 PsiElement endElement = e;
47 while (startElement.getPrevSibling() != null) {
48 PsiElement sibling = startElement.getPrevSibling();
50 if (sibling instanceof PsiJavaToken) {
51 PsiJavaToken token = (PsiJavaToken)sibling;
52 if (token.getTokenType() == JavaTokenType.LBRACE) {
53 break;
57 if (sibling instanceof PsiWhiteSpace) {
58 PsiWhiteSpace whiteSpace = (PsiWhiteSpace)sibling;
60 String[] strings = LineTokenizer.tokenize(whiteSpace.getText().toCharArray(), false);
61 if (strings.length > 2) {
62 break;
66 startElement = sibling;
69 while (startElement instanceof PsiWhiteSpace) {
70 startElement = startElement.getNextSibling();
73 while (endElement.getNextSibling() != null) {
74 PsiElement sibling = endElement.getNextSibling();
76 if (sibling instanceof PsiJavaToken) {
77 PsiJavaToken token = (PsiJavaToken)sibling;
78 if (token.getTokenType() == JavaTokenType.RBRACE) {
79 break;
83 if (sibling instanceof PsiWhiteSpace) {
84 PsiWhiteSpace whiteSpace = (PsiWhiteSpace)sibling;
86 String[] strings = LineTokenizer.tokenize(whiteSpace.getText().toCharArray(), false);
87 if (strings.length > 2) {
88 break;
92 endElement = sibling;
95 while (endElement instanceof PsiWhiteSpace) {
96 endElement = endElement.getPrevSibling();
99 result.addAll(expandToWholeLine(editorText, new TextRange(startElement.getTextRange().getStartOffset(),
100 endElement.getTextRange().getEndOffset())));
102 return result;