<commit_before>package com.intellij.codeInsight.daemon.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorMarkupModel;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.xml.XmlTag;

public class PsiChangeHandler extends PsiTreeChangeAdapter {
  private final Project myProject;
  private final DaemonCodeAnalyzerImpl myDaemonCodeAnalyzer;

  public PsiChangeHandler(Project project, DaemonCodeAnalyzerImpl daemonCodeAnalyzer) {
    myProject = project;
    myDaemonCodeAnalyzer = daemonCodeAnalyzer;
  }

  public void childAdded(PsiTreeChangeEvent event) {
    updateByChange(event.getParent());
  }

  public void childRemoved(PsiTreeChangeEvent event) {
    updateByChange(event.getParent());
  }

  public void childReplaced(PsiTreeChangeEvent event) {
    updateByChange(event.getNewChild());
  }

  public void childrenChanged(PsiTreeChangeEvent event) {
    updateByChange(event.getParent());
  }

  public void beforeChildMovement(PsiTreeChangeEvent event) {
    updateByChange(event.getOldParent());
    updateByChange(event.getNewParent());
  }

  public void propertyChanged(PsiTreeChangeEvent event) {
    String propertyName = event.getPropertyName();
    if (!propertyName.equals(PsiTreeChangeEvent.PROP_WRITABLE)){
      myDaemonCodeAnalyzer.getFileStatusMap().markAllFilesDirty();
      myDaemonCodeAnalyzer.stopProcess(true);
    }
  }

  private void updateByChange(PsiElement child) {
    final Editor editor = FileEditorManager.getInstance(myProject).getSelectedTextEditor();
    if (editor != null) {
      ApplicationManager.getApplication().invokeLater(new Runnable(){
        public void run() {
          EditorMarkupModel markupModel = (EditorMarkupModel) editor.getMarkupModel();
          markupModel.setErrorStripeRenderer(markupModel.getErrorStripeRenderer());
        }
      },ModalityState.stateForComponent(editor.getComponent()));
    }

    PsiFile file = child.getContainingFile();
    if (file == null) {
      myDaemonCodeAnalyzer.getFileStatusMap().markAllFilesDirty();
      return;
    }
    Document document = PsiDocumentManager.getInstance(myProject).getCachedDocument(file);
    if (document == null) return;

    // optimization:
    PsiElement parent = child;
    while (true) {
      if (parent instanceof PsiFile || parent instanceof PsiDirectory) {
        myDaemonCodeAnalyzer.getFileStatusMap().markAllFilesDirty();
        return;
      }
      PsiElement pparent = parent.getParent();
      if(parent instanceof XmlTag){
        //if(!XmlUtil.isInAntBuildFile((XmlFile)parent.getContainingFile())){
          myDaemonCodeAnalyzer.getFileStatusMap().markFileScopeDirty(document, pparent);
          return;
        //}
        //else{
        //  parent = parent.getContainingFile();
        //}
      }
      if (parent instanceof PsiCodeBlock
          && pparent instanceof PsiMethod
          && !((PsiMethod) pparent).isConstructor()
          && pparent.getParent() instanceof PsiClass
          && !(pparent.getParent() instanceof PsiAnonymousClass)) {
        // do not use this optimization for constructors and class initializers - to update non-initialized fields
        myDaemonCodeAnalyzer.getFileStatusMap().markFileScopeDirty(document, pparent);<commit_msg>change in head in html,jsp, etc marks all file dirty<commit_after>package com.intellij.codeInsight.daemon.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorMarkupModel;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.psi.*;
import com.intellij.psi.xml.XmlTag;

public class PsiChangeHandler extends PsiTreeChangeAdapter {
  private final Project myProject;
  private final DaemonCodeAnalyzerImpl myDaemonCodeAnalyzer;

  public PsiChangeHandler(Project project, DaemonCodeAnalyzerImpl daemonCodeAnalyzer) {
    myProject = project;
    myDaemonCodeAnalyzer = daemonCodeAnalyzer;
  }

  public void childAdded(PsiTreeChangeEvent event) {
    updateByChange(event.getParent());
  }

  public void childRemoved(PsiTreeChangeEvent event) {
    updateByChange(event.getParent());
  }

  public void childReplaced(PsiTreeChangeEvent event) {
    updateByChange(event.getNewChild());
  }

  public void childrenChanged(PsiTreeChangeEvent event) {
    updateByChange(event.getParent());
  }

  public void beforeChildMovement(PsiTreeChangeEvent event) {
    updateByChange(event.getOldParent());
    updateByChange(event.getNewParent());
  }

  public void propertyChanged(PsiTreeChangeEvent event) {
    String propertyName = event.getPropertyName();
    if (!propertyName.equals(PsiTreeChangeEvent.PROP_WRITABLE)){
      myDaemonCodeAnalyzer.getFileStatusMap().markAllFilesDirty();
      myDaemonCodeAnalyzer.stopProcess(true);
    }
  }

  private void updateByChange(PsiElement child) {
    final Editor editor = FileEditorManager.getInstance(myProject).getSelectedTextEditor();
    if (editor != null) {
      ApplicationManager.getApplication().invokeLater(new Runnable(){
        public void run() {
          EditorMarkupModel markupModel = (EditorMarkupModel) editor.getMarkupModel();
          markupModel.setErrorStripeRenderer(markupModel.getErrorStripeRenderer());
        }
      },ModalityState.stateForComponent(editor.getComponent()));
    }

    PsiFile file = child.getContainingFile();
    if (file == null) {
      myDaemonCodeAnalyzer.getFileStatusMap().markAllFilesDirty();
      return;
    }
    Document document = PsiDocumentManager.getInstance(myProject).getCachedDocument(file);
    if (document == null) return;

    // optimization:
    PsiElement parent = child;
    while (true) {
      if (parent instanceof PsiFile || parent instanceof PsiDirectory) {
        myDaemonCodeAnalyzer.getFileStatusMap().markAllFilesDirty();
        return;
      }
      PsiElement pparent = parent.getParent();
      
      if(parent instanceof XmlTag){
        PsiElement dirtyScope = pparent;
        
        if (pparent instanceof XmlTag &&
            "head".equals(((XmlTag)pparent).getLocalName())) { 
          final PsiFile containingFile = parent.getContainingFile();
          final FileType fileType = (containingFile != null)? containingFile.getFileType() : null;
          
          if (fileType == StdFileTypes.JSP ||
              fileType == StdFileTypes.JSPX ||
              fileType == StdFileTypes.HTML ||
              fileType == StdFileTypes.XHTML
             ) {
            // change in head will result in changes for css/javascript code highlighting
            dirtyScope = containingFile;
          }
        } 
        
        myDaemonCodeAnalyzer.getFileStatusMap().markFileScopeDirty(document, dirtyScope);
        return;
      }
      
      if (parent instanceof PsiCodeBlock
          && pparent instanceof PsiMethod
          && !((PsiMethod) pparent).isConstructor()
          && pparent.getParent() instanceof PsiClass
          && !(pparent.getParent() instanceof PsiAnonymousClass)) {
        // do not use this optimization for constructors and class initializers - to update non-initialized fields
        myDaemonCodeAnalyzer.getFileStatusMap().markFileScopeDirty(document, pparent);
{'fraction_non_alphanumeric': 0.09507223942208462, 'fraction_numerical': 0.0, 'mean_word_length': 3.7858024691358025, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3402529', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2153, 'n_words': 399}