Skip to content

Commit

Permalink
Added ProvideBraceCompletionAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
TIHan committed Jun 1, 2018
1 parent 273d01a commit 7bd0502
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
20 changes: 6 additions & 14 deletions AutomaticCompletion/BraceCompletionSessionProvider.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ open Microsoft.CodeAnalysis.Classification
[<AutoOpen>]
module BraceCompletionSessionProviderHelpers =

let tryGetLanguageService<'T when 'T :> ILanguageService and 'T : null> (document: Document) =
match document.Project with
| null -> None
| project ->
match project.LanguageServices with
| null -> None
| languageServices ->
languageServices.GetService<'T>()
|> Some

let tryGetCaretPoint (buffer: ITextBuffer) (session: IBraceCompletionSession) =
let point = session.TextView.Caret.Position.Point.GetPoint(buffer, PositionAffinity.Predecessor)
if point.HasValue then Some point.Value
Expand Down Expand Up @@ -94,6 +84,10 @@ type BraceCompletionSession
undoHistory.CreateTransaction(BraceCompletion)

member this.Start (cancellationToken: CancellationToken) =
// Sanity check.
if closingPoint = null then this.EndSession()
else

// this is where the caret should go after the change
let pos = textView.Caret.Position.BufferPosition
let beforeTrackingPoint = pos.Snapshot.CreateTrackingPoint(pos.Position, PointTrackingMode.Negative)
Expand Down Expand Up @@ -534,7 +528,7 @@ type BraceCompletionSessionProvider
session <-
maybe {
let! document = openingPoint.Snapshot.GetOpenDocumentInCurrentContextWithChanges() |> Option.ofObj
let! sessionFactory = tryGetLanguageService<IEditorBraceCompletionSessionFactory> document
let! sessionFactory = document.TryGetLanguageService<IEditorBraceCompletionSessionFactory>()
let! session = sessionFactory.TryCreateSession(document, openingPoint.Position, openingBrace, CancellationToken.None) |> Option.ofObj

let undoHistory = undoManager.GetTextBufferUndoManager(textView.TextBuffer).TextBufferUndoHistory
Expand All @@ -550,6 +544,4 @@ type BraceCompletionSessionProvider
}
|> Option.toObj

match session with
| null -> false
| _ -> true
session <> null
12 changes: 12 additions & 0 deletions Common/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module internal Microsoft.VisualStudio.FSharp.Editor.Extensions
open System
open System.IO
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Host
open Microsoft.FSharp.Compiler.Ast
open Microsoft.FSharp.Compiler.SourceCodeServices

Expand All @@ -28,6 +29,17 @@ type ProjectId with
member this.ToFSharpProjectIdString() =
this.Id.ToString("D").ToLowerInvariant()

type Document with
member this.TryGetLanguageService<'T when 'T :> ILanguageService>() =
match this.Project with
| null -> None
| project ->
match project.LanguageServices with
| null -> None
| languageServices ->
languageServices.GetService<'T>()
|> Some

type FSharpNavigationDeclarationItem with
member x.RoslynGlyph : Glyph =
match x.Glyph with
Expand Down
1 change: 1 addition & 0 deletions Common/Pervasive.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System.IO
open System.Threading.Tasks
open System.Diagnostics

type IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider

/// Checks if the filePath ends with ".fsi"
let isSignatureFile (filePath:string) =
Expand Down
3 changes: 2 additions & 1 deletion FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="Options\SettingsPersistence.fs" />
<Compile Include="Options\UIHelpers.fs" />
<Compile Include="Options\EditorOptions.fs" />
<Compile Include="LanguageService\ProvideBraceCompletionAttribute.fs" />
<Compile Include="LanguageService\FSharpEditorFactory.fs" />
<Compile Include="LanguageService\TextViewCreationListener.fs" />
<Compile Include="LanguageService\Tokenizer.fs" />
Expand Down Expand Up @@ -150,7 +151,7 @@
<PackageReference Include="Microsoft.VisualStudio.Text.UI" Version="$(MicrosoftVisualStudioTextUIPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Text.UI.Wpf" Version="$(MicrosoftVisualStudioTextUIWpfPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop" Version="$(MicrosoftVisualStudioTextManagerInteropPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop.8.0" Version="$(MicrosoftVisualStudioTextManagerInterop80PackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop.12.0" Version="$(MicrosoftVisualStudioTextManagerInterop120PackageVersion)" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="$(MicrosoftVSSDKBuildToolsPackageVersion)" ExcludeAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
<PackageReference Include="System.Collections.Immutable" Version="$(SystemCollectionsImmutablePackageVersion)" />
Expand Down
3 changes: 2 additions & 1 deletion LanguageService/LanguageService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ type internal FSharpCheckerWorkspaceServiceFactory
[<ProvideLanguageExtension(typeof<FSharpLanguageService>, ".fsscript")>]
[<ProvideLanguageExtension(typeof<FSharpLanguageService>, ".ml")>]
[<ProvideLanguageExtension(typeof<FSharpLanguageService>, ".mli")>]
[<ProvideBraceCompletion(FSharpConstants.FSharpLanguageName)>]
[<ProvideLanguageService(languageService = typeof<FSharpLanguageService>,
strLanguageName = FSharpConstants.FSharpLanguageName,
languageResourceID = 100,
Expand Down Expand Up @@ -383,6 +384,7 @@ type internal FSharpPackage() as this =
Microsoft.VisualStudio.FSharp.Interactive.Hooks.fsiConsoleWindowPackageInitalizeSited (this :> Package) commandService
// FSI-LINKAGE-POINT: private method GetDialogPage forces fsi options to be loaded
let _fsiPropertyPage = this.GetDialogPage(typeof<Microsoft.VisualStudio.FSharp.Interactive.FsiPropertyPage>)

()

override this.RoslynLanguageName = FSharpConstants.FSharpLanguageName
Expand Down Expand Up @@ -434,7 +436,6 @@ type internal FSharpLanguageService(package : FSharpPackage) =
override this.Initialize() =
base.Initialize()


let workspaceChanged (args:WorkspaceChangeEventArgs) =
match args.Kind with
| WorkspaceChangeKind.ProjectAdded -> this.OnProjectAdded(args.ProjectId)
Expand Down
17 changes: 17 additions & 0 deletions LanguageService/ProvideBraceCompletionAttribute.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.FSharp.Editor

open System
open Microsoft.VisualStudio.Shell

[<Sealed>]
[<AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)>]
type internal ProvideBraceCompletionAttribute(languageName: string) =
inherit RegistrationAttribute()

override __.Register(context) =
use key = context.CreateKey(@"Languages\Language Services\" + languageName)
key.SetValue("ShowBraceCompletion", 1)

override __.Unregister(_) = ()

0 comments on commit 7bd0502

Please sign in to comment.