<commit_before>/*
 * Copyright 2000-2005 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.vcsUtil;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.localVcs.LocalVcs;
import com.intellij.openapi.localVcs.LocalVcsBundle;
import com.intellij.openapi.localVcs.LvcsObject;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.actions.VcsContext;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.peer.PeerFactory;
import com.intellij.psi.*;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlText;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@SuppressWarnings({"UtilityClassWithoutPrivateConstructor"})
public class VcsUtil
{
  public static void markFileAsUpToDate(VirtualFile file, Project project) {
    markAsUpToDate(project, file.isDirectory(), file.getPath());
    FileStatusManager.getInstance(project).fileStatusChanged(file);
    VcsDirtyScopeManager.getInstance(project).fileDirty(file);
  }

  private static void markAsUpToDate(Project project, boolean directory, String path) {
    LocalVcs localVcs = LocalVcs.getInstance(project);
    LvcsObject lvcsObject;
    if (directory) {
      lvcsObject = localVcs.findDirectory(path, true);
    }
    else {
      lvcsObject = localVcs.findFile(path, true);
    }
    if (lvcsObject != null) {
      lvcsObject.getRevision().setUpToDate(true);
    }
  }

  public static void markFileAsUpToDate(String path, Project project) {
    String canonicalPath = getCanonicalPath(new File(path));
    if (canonicalPath == null) return;
    markAsUpToDate(project, false, canonicalPath);
  }

  public static void markDirectoryAsUpToDate(String path, Project project) {
    String canonicalPath = getCanonicalPath(new File(path));
    if (canonicalPath == null) return;
    markAsUpToDate(project, true, canonicalPath);
  }

  public static String getCanonicalPath(File file) {
    if (SystemInfo.isFileSystemCaseSensitive) {
      return file.getAbsolutePath().replace(File.separatorChar, '/');
    }
    else {
      try {
        return file.getCanonicalPath().replace(File.separatorChar, '/');
      }
      catch (IOException e) {
        return null;
      }
    }
  }

  public static VcsSelection getSelection(VcsContext context) {

    VcsSelection selectionFromEditor = getSelectionFromEditor(context);
    if (selectionFromEditor != null) {
      return selectionFromEditor;
    }
    return getSelectionFromPsiElement(context);
  }
<commit_msg>IDEADEV-9405, refactor marking as dirty into read action<commit_after>/*
 * Copyright 2000-2005 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.vcsUtil;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.localVcs.LocalVcs;
import com.intellij.openapi.localVcs.LocalVcsBundle;
import com.intellij.openapi.localVcs.LvcsObject;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.actions.VcsContext;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.peer.PeerFactory;
import com.intellij.psi.*;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlText;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@SuppressWarnings({"UtilityClassWithoutPrivateConstructor"})
public class VcsUtil
{
  public static void markFileAsUpToDate(VirtualFile file, Project project) {
    markAsUpToDate(project, file.isDirectory(), file.getPath());
    FileStatusManager.getInstance(project).fileStatusChanged(file);
    VcsDirtyScopeManager.getInstance(project).fileDirty(file);
  }

  private static void markAsUpToDate(Project project, boolean directory, String path) {
    LocalVcs localVcs = LocalVcs.getInstance(project);
    LvcsObject lvcsObject;
    if (directory) {
      lvcsObject = localVcs.findDirectory(path, true);
    }
    else {
      lvcsObject = localVcs.findFile(path, true);
    }
    if (lvcsObject != null) {
      lvcsObject.getRevision().setUpToDate(true);
    }
  }

  public static void markFileAsUpToDate(String path, Project project) {
    String canonicalPath = getCanonicalPath(new File(path));
    if (canonicalPath == null) return;
    markAsUpToDate(project, false, canonicalPath);
  }

  public static void markDirectoryAsUpToDate(String path, Project project) {
    String canonicalPath = getCanonicalPath(new File(path));
    if (canonicalPath == null) return;
    markAsUpToDate(project, true, canonicalPath);
  }

  /**
   * Call "fileDirty" in the read action.
   */
  public static void markFileAsDirty( final Project project, final FilePath path )
  {
    final VcsDirtyScopeManager mgr = VcsDirtyScopeManager.getInstance( project );
    ApplicationManager.getApplication().runReadAction( new Runnable() { public void run() { mgr.fileDirty( path ); } } );
  }
  public static String getCanonicalPath(File file) {
    if (SystemInfo.isFileSystemCaseSensitive) {
      return file.getAbsolutePath().replace(File.separatorChar, '/');
    }
    else {
      try {
        return file.getCanonicalPath().replace(File.separatorChar, '/');
      }
      catch (IOException e) {
        return null;
      }
    }
  }

  public static VcsSelection getSelection(VcsContext context) {

    VcsSelection selectionFromEditor = getSelectionFromEditor(context);
    if (selectionFromEditor != null) {
      return selectionFromEditor;
    }
    return getSelectionFromPsiElement(context);
  }

{'fraction_non_alphanumeric': 0.09891662741403674, 'fraction_numerical': 0.003297220913801225, 'mean_word_length': 4.66577718478986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '5098682', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2605, 'n_words': 570}