summaryrefslogtreecommitdiff
path: root/plugins/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/DeleteNodesAction.java
blob: 4b5ad7c645451c654aa1c58f801bf65e17518572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.intellij.structuralsearch.impl.matcher.compiler;

import com.intellij.psi.PsiElement;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: maxim
 * Date: 17.11.2004
 * Time: 19:24:40
 * To change this template use File | Settings | File Templates.
 */
class DeleteNodesAction implements Runnable {
  private final ArrayList elements;

  DeleteNodesAction(ArrayList _elements) {
    elements = _elements;
  }

  private void delete(PsiElement first, PsiElement last) throws Exception {
    if (last==first) {
      first.delete();
    } else {
      first.getParent().deleteChildRange(first,last);
    }
  }
  public void run() {
    try {
      PsiElement first= null;
      PsiElement last = null;

      for(int i = 0;i < elements.size(); ++i) {
        final PsiElement el = (PsiElement)elements.get(i);

        if (!el.isValid()) continue;
          
        if (first==null) {
          first = last = el;
        } else if (last.getNextSibling()==el) {
          last = el;
        } else {
          delete(first,last);
          first = last = null;
          --i;
          continue;
        }
      }

      if (first!=null) {
        delete(first,last);
      }
    } catch(Throwable ex) {
      ex.printStackTrace();
    } finally {
      elements.clear();
    }
  }
}