Skip to content

Commit

Permalink
Use Running Document Table to track open documents
Browse files Browse the repository at this point in the history
Instead of using the ITextDocumentFactoryService component, we now use
IVsRunningDocumentTable to keep track of when files are open and closed
in the VS editor.

The main reason is that ITextDocumentFactoryService keeps track of all
ITextBuffer instances used in VS, including ITextDocument instances
loaded for temporary usage, such as the "Find in File" features.

This change ensures the number of events related to opening and closing
documents is close to zero when using "Find in Files" on large directories
or project.

This is (hopefully) the last part to fix issue #61
  • Loading branch information
rpaquay committed Jun 12, 2020
1 parent 50a8f95 commit b2b493d
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 120 deletions.
8 changes: 7 additions & 1 deletion src/Core/Files/FullPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using VsChromium.Core.Logging;

namespace VsChromium.Core.Files {
Expand All @@ -19,6 +18,13 @@ public FullPath(string path) {
_path = path;
}

public static FullPath? Create(string path) {
if (IsValid(path)) {
return new FullPath(path);
}
return null;
}

public static bool IsValid(string path) {
return PathHelpers.IsAbsolutePath(path);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Core/Files/PathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public static SplitPath SplitPrefix(string path, string prefix) {
/// method should only be used in assertion code.
/// </summary>
public static bool IsAbsolutePath(string path) {
if (string.IsNullOrEmpty(path)) {
return false;
}

// Quick & dirty check, but faster than "Path.IsPathRooted".
var l = path.Length;

Expand Down
9 changes: 7 additions & 2 deletions src/VsChromium/Views/ITextDocumentTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Text;
using VsChromium.Core.Files;

namespace VsChromium.Views {
public interface ITextDocumentTable {
public interface ITextDocumentTable : IDisposable {
ITextDocument GetOpenDocument(FullPath path);
IList<FullPath> GetOpenDocuments();

event EventHandler<TextDocumentEventArgs> TextDocumentOpened;
event EventHandler<TextDocumentEventArgs> TextDocumentClosed;
event EventHandler<VsDocumentRenameEventArgs> TextDocumentRenamed;
}
}
}
57 changes: 0 additions & 57 deletions src/VsChromium/Views/TextDocumentFactoryObserver.cs

This file was deleted.

42 changes: 42 additions & 0 deletions src/VsChromium/Views/TextDocumentRegistrationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text;
using VsChromium.Package;

namespace VsChromium.Views {
[Export(typeof(IPackagePostInitializer))]
public class TextDocumentRegistrationManager : IPackagePostInitializer {
private readonly ITextDocumentTable _textDocumentTable;
private readonly IFileRegistrationRequestService _fileRegistrationRequestService;

[ImportingConstructor]
public TextDocumentRegistrationManager(ITextDocumentTable textDocumentTable, IFileRegistrationRequestService fileRegistrationRequestService) {
_textDocumentTable = textDocumentTable;
_fileRegistrationRequestService = fileRegistrationRequestService;
}

public int Priority { get { return 0; } }

public void Run(IVisualStudioPackage package) {
_textDocumentTable.TextDocumentOpened += TextDocumentFactoryServiceOnTextDocumentOpened;
_textDocumentTable.TextDocumentClosed += TextDocumentFactoryServiceOnTextDocumentClosed;
_textDocumentTable.TextDocumentRenamed += TextTextDocumentFactoryServiceOnTextDocumentRenamed;
}

private void TextDocumentFactoryServiceOnTextDocumentOpened(object sender, TextDocumentEventArgs textDocumentEventArgs) {
_fileRegistrationRequestService.RegisterTextDocument(textDocumentEventArgs.TextDocument);
}

private void TextDocumentFactoryServiceOnTextDocumentClosed(object sender, TextDocumentEventArgs textDocumentEventArgs) {
_fileRegistrationRequestService.UnregisterTextDocument(textDocumentEventArgs.TextDocument);
}

private void TextTextDocumentFactoryServiceOnTextDocumentRenamed(object sender, VsDocumentRenameEventArgs e) {
_fileRegistrationRequestService.RegisterFile(e.NewPath.Value);
_fileRegistrationRequestService.UnregisterFile(e.OldPath.Value);
}
}
}
Loading

0 comments on commit b2b493d

Please sign in to comment.