2 * Copyright 2000-2011 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.actions;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.application.ReadAction;
20 import com.intellij.openapi.application.Result;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.openapi.editor.Document;
23 import com.intellij.openapi.editor.EditorFactory;
24 import com.intellij.openapi.editor.impl.EditorFactoryImpl;
25 import com.intellij.openapi.editor.markup.RangeHighlighter;
26 import com.intellij.openapi.module.ModifiableModuleModel;
27 import com.intellij.openapi.module.Module;
28 import com.intellij.openapi.module.ModuleManager;
29 import com.intellij.openapi.project.Project;
30 import com.intellij.openapi.roots.ModuleRootManager;
31 import com.intellij.openapi.util.Key;
32 import com.intellij.openapi.util.TextRange;
33 import com.intellij.openapi.vcs.VcsException;
34 import com.intellij.openapi.vcs.changes.Change;
35 import com.intellij.openapi.vcs.changes.ChangeListManager;
36 import com.intellij.openapi.vcs.changes.ContentRevision;
37 import com.intellij.openapi.vcs.ex.LineStatusTracker;
38 import com.intellij.openapi.vcs.ex.Range;
39 import com.intellij.openapi.vcs.ex.RangesBuilder;
40 import com.intellij.openapi.vcs.impl.LineStatusTrackerManager;
41 import com.intellij.openapi.vcs.impl.LineStatusTrackerManagerI;
42 import com.intellij.openapi.vfs.VirtualFile;
43 import com.intellij.psi.PsiDirectory;
44 import com.intellij.psi.PsiDocumentManager;
45 import com.intellij.psi.PsiFile;
46 import com.intellij.psi.PsiManager;
47 import com.intellij.util.Function;
48 import com.intellij.util.containers.ContainerUtil;
49 import com.intellij.util.containers.ContainerUtilRt;
50 import com.intellij.util.diff.FilesTooBigForDiffException;
51 import org.jetbrains.annotations.NotNull;
52 import org.jetbrains.annotations.Nullable;
56 public class FormatChangedTextUtil {
57 public static final Key<String> TEST_REVISION_CONTENT = Key.create("test.revision.content");
58 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.actions.FormatChangedTextUtil");
59 protected static final Key<List<TextRange>> CHANGED_RANGES = Key.create("changed.ranges.since.last.revision");
62 private FormatChangedTextUtil() {
66 * Allows to answer if given file has changes in comparison with VCS.
68 * @param file target file
69 * @return <code>true</code> if given file has changes; <code>false</code> otherwise
71 public static boolean hasChanges(@NotNull PsiFile file) {
72 final Project project = file.getProject();
73 final VirtualFile virtualFile = file.getVirtualFile();
74 if (virtualFile != null) {
75 final Change change = ChangeListManager.getInstance(project).getChange(virtualFile);
76 return change != null;
82 * Allows to answer if any file below the given directory (any level of nesting) has changes in comparison with VCS.
84 * @param directory target directory to check
85 * @return <code>true</code> if any file below the given directory has changes in comparison with VCS;
86 * <code>false</code> otherwise
88 public static boolean hasChanges(@NotNull PsiDirectory directory) {
89 return hasChanges(directory.getVirtualFile(), directory.getProject());
93 * Allows to answer if given file or any file below the given directory (any level of nesting) has changes in comparison with VCS.
95 * @param file target directory to check
96 * @param project target project
97 * @return <code>true</code> if given file or any file below the given directory has changes in comparison with VCS;
98 * <code>false</code> otherwise
100 public static boolean hasChanges(@NotNull VirtualFile file, @NotNull Project project) {
101 final Collection<Change> changes = ChangeListManager.getInstance(project).getChangesIn(file);
102 for (Change change : changes) {
103 if (change.getType() == Change.Type.NEW || change.getType() == Change.Type.MODIFICATION) {
110 public static boolean hasChanges(@NotNull VirtualFile[] files, @NotNull Project project) {
111 for (VirtualFile file : files) {
112 if (hasChanges(file, project))
119 * Allows to answer if any file that belongs to the given module has changes in comparison with VCS.
121 * @param module target module to check
122 * @return <code>true</code> if any file that belongs to the given module has changes in comparison with VCS
123 * <code>false</code> otherwise
125 public static boolean hasChanges(@NotNull Module module) {
126 final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
127 for (VirtualFile root : rootManager.getSourceRoots()) {
128 if (hasChanges(root, module.getProject())) {
136 * Allows to answer if any file that belongs to the given project has changes in comparison with VCS.
138 * @param project target project to check
139 * @return <code>true</code> if any file that belongs to the given project has changes in comparison with VCS
140 * <code>false</code> otherwise
142 public static boolean hasChanges(@NotNull final Project project) {
143 final ModifiableModuleModel moduleModel = new ReadAction<ModifiableModuleModel>() {
145 protected void run(Result<ModifiableModuleModel> result) throws Throwable {
146 result.setResult(ModuleManager.getInstance(project).getModifiableModel());
148 }.execute().getResultObject();
150 for (Module module : moduleModel.getModules()) {
151 if (hasChanges(module)) {
158 moduleModel.dispose();
163 * Allows to ask for the changed text of the given file (in comparison with VCS).
165 * @param file target file
166 * @return collection of changed regions for the given file
169 public static Collection<TextRange> getChanges(@NotNull PsiFile file) {
170 final Set<TextRange> defaultResult = Collections.singleton(file.getTextRange());
171 final VirtualFile virtualFile = file.getVirtualFile();
172 if (virtualFile != null) {
173 final Change change = ChangeListManager.getInstance(file.getProject()).getChange(virtualFile);
174 if (change != null && change.getType() == Change.Type.NEW) {
175 return defaultResult;
179 final LineStatusTrackerManagerI manager = LineStatusTrackerManager.getInstance(file.getProject());
180 if (manager == null) {
181 return defaultResult;
183 final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
184 if (document == null) {
185 return defaultResult;
187 final LineStatusTracker lineStatusTracker = manager.getLineStatusTracker(document);
188 if (lineStatusTracker == null) {
189 return defaultResult;
191 final List<Range> ranges = lineStatusTracker.getRanges();
192 if (ranges == null || ranges.isEmpty()) {
193 return defaultResult;
196 List<TextRange> result = new ArrayList<TextRange>();
197 for (Range range : ranges) {
198 if (range.getType() != Range.DELETED) {
199 final RangeHighlighter highlighter = range.getHighlighter();
200 if (highlighter != null) {
201 result.add(new TextRange(highlighter.getStartOffset(), highlighter.getEndOffset()));
209 public static List<PsiFile> getChangedFilesFromDirs(@NotNull Project project, @NotNull List<PsiDirectory> dirs) {
210 ChangeListManager changeListManager = ChangeListManager.getInstance(project);
211 Collection<Change> changes = ContainerUtil.newArrayList();
213 for (PsiDirectory dir : dirs) {
214 changes.addAll(changeListManager.getChangesIn(dir.getVirtualFile()));
217 return getChangedFiles(project, changes);
221 public static List<PsiFile> getChangedFiles(@NotNull final Project project, @NotNull Collection<Change> changes) {
222 Function<Change, PsiFile> changeToPsiFileMapper = new Function<Change, PsiFile>() {
223 private PsiManager myPsiManager = PsiManager.getInstance(project);
226 public PsiFile fun(Change change) {
227 VirtualFile vFile = change.getVirtualFile();
228 return vFile != null ? myPsiManager.findFile(vFile) : null;
232 return ContainerUtil.mapNotNull(changes, changeToPsiFileMapper);
236 public static List<TextRange> getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) throws FilesTooBigForDiffException {
237 List<TextRange> cachedChangedLines = getCachedChangedLines(project, file);
238 if (cachedChangedLines != null) {
239 return cachedChangedLines;
242 if (ApplicationManager.getApplication().isUnitTestMode()) {
243 String testContent = file.getUserData(TEST_REVISION_CONTENT);
244 if (testContent != null) {
245 return calculateChangedTextRanges(file.getProject(), file, testContent);
249 Change change = ChangeListManager.getInstance(project).getChange(file.getVirtualFile());
250 if (change == null) {
251 return ContainerUtilRt.emptyList();
253 if (change.getType() == Change.Type.NEW) {
254 return ContainerUtil.newArrayList(file.getTextRange());
257 String contentFromVcs = getRevisionedContentFrom(change);
258 return contentFromVcs != null ? calculateChangedTextRanges(document, contentFromVcs)
259 : ContainerUtil.<TextRange>emptyList();
263 private static List<TextRange> getCachedChangedLines(@NotNull Project project, @NotNull Document document) {
264 LineStatusTracker tracker = LineStatusTrackerManager.getInstance(project).getLineStatusTracker(document);
265 if (tracker != null) {
266 List<Range> ranges = tracker.getRanges();
267 return getChangedTextRanges(document, ranges);
274 private static String getRevisionedContentFrom(@NotNull Change change) {
275 ContentRevision revision = change.getBeforeRevision();
276 if (revision == null) {
281 return revision.getContent();
283 catch (VcsException e) {
284 LOG.error("Can't get content for: " + change.getVirtualFile(), e);
290 protected static List<TextRange> calculateChangedTextRanges(@NotNull Document document,
291 @NotNull CharSequence contentFromVcs) throws FilesTooBigForDiffException
293 Document documentFromVcs = ((EditorFactoryImpl)EditorFactory.getInstance()).createDocument(contentFromVcs, true, false);
294 List<Range> changedRanges = new RangesBuilder(document, documentFromVcs).getRanges();
295 return getChangedTextRanges(document, changedRanges);
299 private static List<TextRange> getChangedTextRanges(@NotNull Document document, @NotNull List<Range> changedRanges) {
300 List<TextRange> ranges = ContainerUtil.newArrayList();
301 for (Range range : changedRanges) {
302 if (range.getType() != Range.DELETED) {
303 int changeStartLine = range.getLine1();
304 int changeEndLine = range.getLine2();
306 int lineStartOffset = document.getLineStartOffset(changeStartLine);
307 int lineEndOffset = document.getLineEndOffset(changeEndLine - 1);
309 ranges.add(new TextRange(lineStartOffset, lineEndOffset));