Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting for multi-line triple-quoted string content #1300

Open
5 of 6 tasks
Thorium opened this issue Aug 4, 2023 · 7 comments
Open
5 of 6 tasks

Syntax highlighting for multi-line triple-quoted string content #1300

Thorium opened this issue Aug 4, 2023 · 7 comments

Comments

@Thorium
Copy link

Thorium commented Aug 4, 2023

I propose we should have a way to do syntax highlighting (colour coding) of different content inside multi-line triple-quoted string.

Like markdown has: ```json or html and so on:

It might not be optimal to have other content in F# files, typically many times it's better to read separate external file. But also it's a bit over-kill to do file-operations every time. So you might see things like:

let emailContent = """<html><head>...

or

type myRequest= FSharp.Data.JsonProvider< """[{ ...

...and then some 10-50 lines of single colour text without any intellisense or text correction. Hard to find any typos there.

Now, I don't know if this is purely editor thing or language feature: I suspect editors cannot infer content types without the core language change first. A lot of editors use things like compiler service to do things.

Pros and Cons

The advantages of making this adjustment to F# are:

  • Less typos in sometimes strictly formatted content, reduce issues bugs and debug time.

The disadvantages of making this adjustment to F# are:

  • Possibly making already slow intellisense loading even slower in Visual Studio. So this should be the lowest priority background task.
  • May lead more string-parsing code
  • If there would be also fsharp option support within content, this could lead to yet another way to do easier unwanted metaprogramming, macros, recursion, etc.

Extra information

I don't know the implementation details, what would be the best way to hint the file content type. Most editors already can do intellisense for basic types like html and json. So maybe something like this:

//markdown style
let mystr = """html
<html>..."""

// or maybe parsing a comment or directive:
//#html
let mystr = """<html>

// or maybe like attribute?
[<Content("html">]
let mystr = """...

Affidavit

Please tick these items by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
  • This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository (?)
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

@vzarytovskii
Copy link

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.stringsyntaxattribute?view=net-7.0 can be used.

Also, it's rather tooling ask, should probably be over dotnet/fsharp.

@Happypig375
Copy link
Contributor

Also it's best not to break the interpretation of triple-quoted strings. Instead, we may do this for longer-quoted strings: #1301

let mystr = """""""html
    <html>
        <body>
            <h1>Heading</h1>
            <p>Paragraph</p>
        </body>
    </html>
"""""""

@cmeeren
Copy link

cmeeren commented Aug 6, 2023

For F#, this now is supported in Rider 2023.2 (they call it "language injections").

image

Works for any string:

image

@Hardt-Coded
Copy link

Hardt-Coded commented Aug 7, 2023

Is it possible to inject language highlighting into visual studio, easy enough?
Because, for my extension, I had to implement the html highlighting and use the code from @madskristensen 's and use it as a base for the f# html highlighting. (and it's implemented from scratch!)
In vscode I heard it's easier, because they have also this language injection.

Here is the VS Extension for html highlighting, but as I sad, highlighting implemented from scratch. But I have no deep knowledge to extend Visual Studio. So maybe I did this wrong!
https://github.com/DieselMeister/HtmlForFSharp

@baronfel
Copy link
Contributor

baronfel commented Aug 7, 2023

There's a VScode extension for a subset of this feature, yes: https://marketplace.visualstudio.com/items?itemName=alfonsogarciacaro.vscode-template-fsharp-highlight

Ideally most uses of this feature could be subsumed by tooling recognizing StringSyntaxAttribute - it's already used by the BCL in many places to inject sub-grammars (for example dates and regex).

@cmeeren
Copy link

cmeeren commented Aug 7, 2023

FWIW, Rider already works with StringSyntaxAttribute, too (e.g. for regex highlighting).

@vzarytovskii
Copy link

Is it possible to inject language highlighting into visual studio, easy enough?
Because, for my extension, I had to implement the html highlighting and use the code from @madskristensen 's and use it as a base for the f# html highlighting. (and it's implemented from scratch!)
In vscode I heard it's easier, because they have also this language injection.

Here is the VS Extension for html highlighting, but as I sad, highlighting implemented from scratch. But I have no deep knowledge to extend Visual Studio. So maybe I did this wrong!
https://github.com/DieselMeister/HtmlForFSharp

We highlight ourselves, by spans (both syntactic and semantic), should be easy enough for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants