razor icon indicating copy to clipboard operation
razor copied to clipboard

Consolidate `RazorSourceDocument` and `SourceText`

Open davidwengier opened this issue 2 years ago • 5 comments

Currently the compiler produces RazorSourceDocuments which bear a passing resemblance to Roslyn's SourceText class, though with less features around dealing with lines and line spans etc. In order to take advantage of those features, in tooling we often convert that RazorSourceDocument to a SourceText by copying the buffer array, then converting it to a string, then creating a SourceText. All of that is inefficient and a LOH risk, and seems unnecessary.

We should fix this in some way, either:

  • Have RazorSourceDocument simply wrap a smarter SourceText class, and allow direct access to it for tooling
    • We can't remove RazorSourceDocument because its part of the public API
    • Worth noting that other parts of Razor tooling already use SourceText, for example the DocumentSnapshot system
  • Fill in the gaps between RazorSourceDocument and SourceText and have tooling stop doing the conversion
    • I prototyped this for RazorDocumentMappingService and its not difficult, but we would miss out on any optimizations Roslyn had made, if we reimplement the API

Thoughts @DustinCampbell @333fred ?

davidwengier avatar Jan 16 '23 21:01 davidwengier

I prefer the first approach, especially since the public API here is something that we will end up breaking at some point.

333fred avatar Jan 17 '23 17:01 333fred

I'm also in favor of the first approach. I'd rather defer to the existing Roslyn abstraction rather than augmenting the existing API. We can do this internally for tooling.

DustinCampbell avatar Jan 18 '23 18:01 DustinCampbell

A 3rd option, one I think I'm most in favor of:

  • Delete RazorSourceDocument, and use SourceText directly.

333fred avatar Jul 06 '23 23:07 333fred

I'm not sure I love that 3rd option. Having some sort of higher-level document class is valuable for hanging additional data on. SourceText is just meant to represent a text.

DustinCampbell avatar Jul 10 '23 16:07 DustinCampbell

Having some sort of higher-level document class is valuable for hanging additional data on. SourceText is just meant to represent a text.

A RazorSourceDocument is only SourceText + FilePath. Locally, I have a change where I've been moving it to exactly that state, but at some point we may want to consolidate RazorSourceDocument and RazorCodeDocument. I'm not sure having a distinction between those is particularly useful :).

333fred avatar Jul 10 '23 16:07 333fred

Option 1 has been completed.

davidwengier avatar Jun 18 '24 05:06 davidwengier