BlockStructureService.fs 4.4 KB
Newer Older
1 2
// Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

J
Jared Hester 已提交
3
namespace Microsoft.VisualStudio.FSharp.Editor
4 5 6 7 8 9 10 11 12 13 14 15 16

open System.Composition
open System.Collections.Immutable
open System.Threading.Tasks

open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Host.Mef
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.Structure

open Microsoft.FSharp.Compiler
open Microsoft.FSharp.Compiler.Range
open Microsoft.FSharp.Compiler.SourceCodeServices
J
Jared Hester 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
open Microsoft.VisualStudio.FSharp.Editor.Structure

module internal BlockStructure =
    let scopeToBlockType = function
    | Scope.Open -> BlockTypes.Imports
    | Scope.Namespace
    | Scope.Module -> BlockTypes.Namespace 
    | Scope.Record
    | Scope.Interface
    | Scope.TypeExtension
    | Scope.RecordDefn
    | Scope.CompExpr
    | Scope.ObjExpr
    | Scope.UnionDefn
    | Scope.Attribute
    | Scope.Type -> BlockTypes.Type
    | Scope.New
    | Scope.RecordField
    | Scope.Member -> BlockTypes.Member
    | Scope.LetOrUse
    | Scope.Match
    | Scope.MatchClause
    | Scope.EnumCase
    | Scope.UnionCase
    | Scope.MatchLambda
    | Scope.ThenInIfThenElse
    | Scope.ElseInIfThenElse
    | Scope.TryWith
    | Scope.TryInTryWith
    | Scope.WithInTryWith
    | Scope.TryFinally
    | Scope.TryInTryFinally
    | Scope.FinallyInTryFinally
    | Scope.IfThenElse-> BlockTypes.Conditional
    | Scope.Tuple
    | Scope.ArrayOrList
    | Scope.CompExprInternal
    | Scope.Quote
    | Scope.SpecialFunc
    | Scope.Lambda
    | Scope.LetOrUseBang
    | Scope.Val
    | Scope.YieldOrReturn
    | Scope.YieldOrReturnBang
    | Scope.TryWith -> BlockTypes.Expression
    | Scope.Do -> BlockTypes.Statement
    | Scope.While
    | Scope.For -> BlockTypes.Loop
    | Scope.HashDirective -> BlockTypes.PreprocessorRegion
    | Scope.Comment
    | Scope.XmlDocComment -> BlockTypes.Comment

    let createBlockSpans (sourceText:SourceText) (parsedInput:Ast.ParsedInput) =
        let linetext = sourceText.Lines |> Seq.map (fun x -> x.ToString()) |> Seq.toArray
        
        Structure.getOutliningRanges linetext parsedInput
        |> Seq.distinctBy (fun x -> x.Range.StartLine)
        |> Seq.choose (fun scopeRange -> 
            // the range of text to collapse
J
Jared Hester 已提交
76
            let textSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, scopeRange.CollapseRange)
J
Jared Hester 已提交
77
            // the range of the entire expression
J
Jared Hester 已提交
78
            let hintSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, scopeRange.Range)
J
Jared Hester 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
            match textSpan,hintSpan with
            | Some textSpan, Some hintSpan ->
                let line = sourceText.Lines.GetLineFromPosition  textSpan.Start
                let bannerText =
                    match Option.ofNullable (line.Span.Intersection textSpan) with
                    | Some span -> sourceText.GetSubText(span).ToString()+"..."
                    | None -> "..."

                Some <| (BlockSpan(scopeToBlockType scopeRange.Scope, true, textSpan,hintSpan,bannerText):BlockSpan)
            | _, _ -> None
        )

open BlockStructure
92 93 94 95
 
type internal FSharpBlockStructureService(checker: FSharpChecker, projectInfoManager: ProjectInfoManager) =
    inherit BlockStructureService()
        
J
Jared Hester 已提交
96
    override __.Language = FSharpConstants.FSharpLanguageName
97 98
 
    override __.GetBlockStructureAsync(document, cancellationToken) : Task<BlockStructure> =
99 100 101 102
        asyncMaybe {
            let! options = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document)
            let! sourceText = document.GetTextAsync(cancellationToken)
            let! parsedInput = checker.ParseDocument(document, options, sourceText)
103
            return createBlockSpans sourceText parsedInput |> Seq.toImmutableArray
104 105 106
        } 
        |> Async.map (Option.defaultValue ImmutableArray<_>.Empty)
        |> Async.map BlockStructure
J
Jared Hester 已提交
107
        |> RoslynHelpers.StartAsyncAsTask(cancellationToken)
J
Jared Hester 已提交
108

J
Jared Hester 已提交
109
[<ExportLanguageServiceFactory(typeof<BlockStructureService>, FSharpConstants.FSharpLanguageName); Shared>]
J
Jared Hester 已提交
110 111 112 113
type internal FSharpBlockStructureServiceFactory [<ImportingConstructor>](checkerProvider: FSharpCheckerProvider, projectInfoManager: ProjectInfoManager) =
    interface ILanguageServiceFactory with
        member __.CreateLanguageService(_languageServices) =
            upcast FSharpBlockStructureService(checkerProvider.Checker, projectInfoManager)