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

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.WeakHashMap;

import java.util.Map;

public class FileStatusMap {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.FileStatusMap");

  private final Project myProject;

  private final Map<Document,FileStatus> myDocumentToStatusMap = new WeakHashMap<Document, FileStatus>(); // all dirty if absent

  private final Key<RefCountHolder> REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY = Key.create("DaemonCodeAnalyzerImpl.REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY");
  private Map<Document,Object> myDocumentsWithRefCountHolders = new WeakHashMap<Document, Object>(); // Document --> null
  private final Object myRefCountHolderLock = new Object();

  private static class FileStatus {
    PsiElement dirtyScope; //Q: use WeakReference?
    PsiElement overridenDirtyScope;
    PsiElement localInspectionsDirtyScope;

    FileStatus(PsiElement dirtyScope, PsiElement overridenDirtyScope, PsiElement inspectionDirtyScope) {
      this.dirtyScope = dirtyScope;
      this.overridenDirtyScope = overridenDirtyScope;
      localInspectionsDirtyScope = inspectionDirtyScope;
    }
  }

  public static final int NORMAL_HIGHLIGHTERS = 1;
  public static final int OVERRIDEN_MARKERS = 2;
  public static final int LOCAL_INSPECTIONS = 3;

  public FileStatusMap(Project project) {
    myProject = project;
  }

  public void markAllFilesDirty() {
    synchronized(myDocumentToStatusMap){
      myDocumentToStatusMap.clear();
    }

    synchronized(myRefCountHolderLock){
      for (Document document : myDocumentsWithRefCountHolders.keySet()) {
        document.putUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY, null);
      }
      myDocumentsWithRefCountHolders.clear();
    }
  }

  public void markFileDirty(Document document) {
    synchronized(myDocumentToStatusMap){
      myDocumentToStatusMap.remove(document);
    }

    synchronized(myRefCountHolderLock){
      document.putUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY, null);
      myDocumentsWithRefCountHolders.remove(document);
    }
  }

  public void markFileUpToDate(Document document, int part) {
    synchronized(myDocumentToStatusMap){
      FileStatus status = myDocumentToStatusMap.get(document);
      if (status == null){
        PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
        status = new FileStatus(file, file, file);
        myDocumentToStatusMap.put(document, status);
      }

      if (part == NORMAL_HIGHLIGHTERS){
        status.dirtyScope = null;
      }
      else if (part == OVERRIDEN_MARKERS){
        status.overridenDirtyScope = null;
      }
      else if (part == LOCAL_INSPECTIONS){
        status.localInspectionsDirtyScope = null;
      }
      else{
        LOG.assertTrue(false);
      }
    }
  }

  public PsiElement getFileDirtyScope(Document document, int part) {
    synchronized(myDocumentToStatusMap){
      FileStatus status = myDocumentToStatusMap.get(document);
      if (status == null){
        return PsiDocumentManager.getInstance(myProject).getPsiFile(document);
      }
      if (part == NORMAL_HIGHLIGHTERS){
        return status.dirtyScope;
      }
      else if (part == OVERRIDEN_MARKERS){
        return status.overridenDirtyScope;
      }
      else if (part == LOCAL_INSPECTIONS) {
        return status.localInspectionsDirtyScope;
      }
      else{
        LOG.assertTrue(false);
        return null;
      }
    }
  }

  public void markFileScopeDirty(Document document, PsiElement scope) {
    synchronized(myDocumentToStatusMap){
      FileStatus status = myDocumentToStatusMap.get(document);
      if (status == null) return; // all dirty already
      final PsiElement combined1 = combineScopes(status.dirtyScope, scope);
      status.dirtyScope = combined1 == null ? PsiDocumentManager.getInstance(myProject).getPsiFile(document) : combined1;
      final PsiElement combined2 = combineScopes(status.localInspectionsDirtyScope, scope);
      status.localInspectionsDirtyScope = combined2 == null ? PsiDocumentManager.getInstance(myProject).getPsiFile(document) : combined2;
      //status.overridenDirtyScope = combineScopes(status.overridenDirtyScope, scope);
    }
  }

  private static PsiElement combineScopes(PsiElement scope1, PsiElement scope2) {
    if (scope1 == null) return scope2;
    if (scope2 == null) return scope1;
    final PsiElement commonParent = PsiTreeUtil.findCommonParent(scope1, scope2);
    if (commonParent instanceof PsiDirectory) return null;
    return commonParent;
  }

  public RefCountHolder getRefCountHolder(Document document, PsiFile file) {
    RefCountHolder refCountHolder;
    synchronized (myRefCountHolderLock) {
      refCountHolder = document.getUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY);
      if (refCountHolder == null) {
        refCountHolder = new RefCountHolder(file);
        document.putUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY, refCountHolder);
        myDocumentsWithRefCountHolders.put(document, null);
      }
    }
    return refCountHolder;
  }
}<commit_msg>fixed invalidelementaccess<commit_after>
package com.intellij.codeInsight.daemon.impl;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.WeakHashMap;

import java.util.Map;

public class FileStatusMap {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.FileStatusMap");

  private final Project myProject;

  private final Map<Document,FileStatus> myDocumentToStatusMap = new WeakHashMap<Document, FileStatus>(); // all dirty if absent

  private final Key<RefCountHolder> REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY = Key.create("DaemonCodeAnalyzerImpl.REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY");
  private Map<Document,Object> myDocumentsWithRefCountHolders = new WeakHashMap<Document, Object>(); // Document --> null
  private final Object myRefCountHolderLock = new Object();

  private static class FileStatus {
    private PsiElement dirtyScope; //Q: use WeakReference?
    private PsiElement overridenDirtyScope;
    private PsiElement localInspectionsDirtyScope;

    private FileStatus(PsiElement dirtyScope, PsiElement overridenDirtyScope, PsiElement inspectionDirtyScope) {
      this.dirtyScope = dirtyScope;
      this.overridenDirtyScope = overridenDirtyScope;
      localInspectionsDirtyScope = inspectionDirtyScope;
    }
  }

  public static final int NORMAL_HIGHLIGHTERS = 1;
  public static final int OVERRIDEN_MARKERS = 2;
  public static final int LOCAL_INSPECTIONS = 3;

  public FileStatusMap(Project project) {
    myProject = project;
  }

  public void markAllFilesDirty() {
    synchronized(myDocumentToStatusMap){
      myDocumentToStatusMap.clear();
    }

    synchronized(myRefCountHolderLock){
      for (Document document : myDocumentsWithRefCountHolders.keySet()) {
        document.putUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY, null);
      }
      myDocumentsWithRefCountHolders.clear();
    }
  }

  public void markFileDirty(Document document) {
    synchronized(myDocumentToStatusMap){
      myDocumentToStatusMap.remove(document);
    }

    synchronized(myRefCountHolderLock){
      document.putUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY, null);
      myDocumentsWithRefCountHolders.remove(document);
    }
  }

  public void markFileUpToDate(Document document, int part) {
    synchronized(myDocumentToStatusMap){
      FileStatus status = myDocumentToStatusMap.get(document);
      if (status == null){
        PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
        status = new FileStatus(file, file, file);
        myDocumentToStatusMap.put(document, status);
      }

      if (part == NORMAL_HIGHLIGHTERS){
        status.dirtyScope = null;
      }
      else if (part == OVERRIDEN_MARKERS){
        status.overridenDirtyScope = null;
      }
      else if (part == LOCAL_INSPECTIONS){
        status.localInspectionsDirtyScope = null;
      }
      else{
        LOG.assertTrue(false);
      }
    }
  }

  public PsiElement getFileDirtyScope(Document document, int part) {
    synchronized(myDocumentToStatusMap){
      FileStatus status = myDocumentToStatusMap.get(document);
      if (status == null){
        return PsiDocumentManager.getInstance(myProject).getPsiFile(document);
      }
      if (part == NORMAL_HIGHLIGHTERS){
        return status.dirtyScope;
      }
      else if (part == OVERRIDEN_MARKERS){
        return status.overridenDirtyScope;
      }
      else if (part == LOCAL_INSPECTIONS) {
        return status.localInspectionsDirtyScope;
      }
      else{
        LOG.assertTrue(false);
        return null;
      }
    }
  }

  public void markFileScopeDirty(Document document, PsiElement scope) {
    synchronized(myDocumentToStatusMap){
      FileStatus status = myDocumentToStatusMap.get(document);
      if (status == null) return; // all dirty already
      final PsiElement combined1 = combineScopes(status.dirtyScope, scope);
      status.dirtyScope = combined1 == null ? PsiDocumentManager.getInstance(myProject).getPsiFile(document) : combined1;
      final PsiElement combined2 = combineScopes(status.localInspectionsDirtyScope, scope);
      status.localInspectionsDirtyScope = combined2 == null ? PsiDocumentManager.getInstance(myProject).getPsiFile(document) : combined2;
      //status.overridenDirtyScope = combineScopes(status.overridenDirtyScope, scope);
    }
  }

  private static PsiElement combineScopes(PsiElement scope1, PsiElement scope2) {
    if (scope1 == null) return scope2;
    if (scope2 == null) return scope1;
    if (!scope1.isValid() || !scope2.isValid()) return null;
    final PsiElement commonParent = PsiTreeUtil.findCommonParent(scope1, scope2);
    if (commonParent instanceof PsiDirectory) return null;
    return commonParent;
  }

  public RefCountHolder getRefCountHolder(Document document, PsiFile file) {
    RefCountHolder refCountHolder;
    synchronized (myRefCountHolderLock) {
      refCountHolder = document.getUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY);
      if (refCountHolder == null) {
        refCountHolder = new RefCountHolder(file);
        document.putUserData(REF_COUND_HOLDER_IN_EDITOR_DOCUMENT_KEY, refCountHolder);
        myDocumentsWithRefCountHolders.put(document, null);
      }
    }
    return refCountHolder;
  }
}
{'fraction_non_alphanumeric': 0.08504319654427646, 'fraction_numerical': 0.0032397408207343412, 'mean_word_length': 4.074429223744292, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '6204291', 'n_tokens_mistral': 3385, 'n_tokens_neox': 3341, 'n_words': 632}