Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions vsintegration/src/FSharp.Editor/Common/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ type Project with

type TextViewEventsHandler
(
onChangeCaretHandler: (IVsTextView * int * int -> unit) option,
onKillFocus: (IVsTextView -> unit) option,
onSetFocus: (IVsTextView -> unit) option
onChangeCaretHandler: (IVsTextView * int * int -> unit) voption,
onKillFocus: (IVsTextView -> unit) voption,
onSetFocus: (IVsTextView -> unit) voption
) =
interface IVsTextViewEvents with
member this.OnChangeCaretLine(view: IVsTextView, newline: int, oldline: int) =
onChangeCaretHandler
|> Option.iter (fun handler -> handler (view, newline, oldline))
|> ValueOption.iter (fun handler -> handler (view, newline, oldline))

member this.OnChangeScrollInfo
(_view: IVsTextView, _iBar: int, _iMinUnit: int, _iMaxUnits: int, _iVisibleUnits: int, _iFirstVisibleUnit: int)
=
()

member this.OnKillFocus(view: IVsTextView) =
onKillFocus |> Option.iter (fun handler -> handler (view))
onKillFocus |> ValueOption.iter (fun handler -> handler (view))

member this.OnSetBuffer(_view: IVsTextView, _buffer: IVsTextLines) = ()

member this.OnSetFocus(view: IVsTextView) =
onSetFocus |> Option.iter (fun handler -> handler (view))
onSetFocus |> ValueOption.iter (fun handler -> handler (view))

type ConnectionPointSubscription = System.IDisposable option
type ConnectionPointSubscription = System.IDisposable voption

// Usage example:
// If a handler is None, to not handle that event
Expand All @@ -108,16 +108,16 @@ let subscribeToTextViewEvents (textView: IVsTextView, onChangeCaretHandler, onKi
let mutable cookie = 0u

match cpContainer.FindConnectionPoint(ref riid) with
| null -> None
| null -> ValueNone
| cp ->
Some(
ValueSome(
cp.Advise(handler, &cookie)

{ new IDisposable with
member _.Dispose() = cp.Unadvise(cookie)
}
)
| _ -> None
| _ -> ValueNone

type Document with

Expand All @@ -129,7 +129,7 @@ type Document with
| null -> None
| languageServices -> languageServices.GetService<'T>() |> Some

member this.TryGetIVsTextView() : IVsTextView option =
member this.TryGetIVsTextView() : IVsTextView voption =
match ServiceProvider.GlobalProvider.GetService(typeof<SVsTextManager>) with
| :? IVsTextManager as textManager ->
// Grab IVsRunningDocumentTable
Expand All @@ -140,20 +140,20 @@ type Document with
match Marshal.GetObjectForIUnknown docData with
| :? IVsTextBuffer as ivsTextBuffer ->
match textManager.GetActiveView(0, ivsTextBuffer) with
| hr, vsTextView when ErrorHandler.Succeeded(hr) -> Some vsTextView
| _ -> None
| _ -> None
| _ -> None
| _ -> None
| _ -> None

member this.TryGetTextViewAndCaretPos() : (IVsTextView * Position) option =
| hr, vsTextView when ErrorHandler.Succeeded(hr) -> ValueSome vsTextView
| _ -> ValueNone
| _ -> ValueNone
| _ -> ValueNone
| _ -> ValueNone
| _ -> ValueNone

member this.TryGetTextViewAndCaretPos() : (IVsTextView * Position) voption =
match this.TryGetIVsTextView() with
| Some textView ->
| ValueSome textView ->
match textView.GetCaretPos() with
| hr, line, column when ErrorHandler.Succeeded(hr) -> Some(textView, Position.fromZ line column)
| _ -> None
| None -> None
| hr, line, column when ErrorHandler.Succeeded(hr) -> ValueSome(textView, Position.fromZ line column)
| _ -> ValueNone
| ValueNone -> ValueNone

member this.IsFSharpScript = isScriptFile this.FilePath

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
cancellableTask {
let! ct = CancellableTask.getCancellationToken ()
let! fileStamp = document.GetTextVersionAsync(ct)
let textViewAndCaret () : (IVsTextView * Position) option = document.TryGetTextViewAndCaretPos()
let textViewAndCaret () : (IVsTextView * Position) voption = document.TryGetTextViewAndCaretPos()

match singleFileCache.TryGetValue(document.Id) with
| false, _ ->
Expand All @@ -214,7 +214,7 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
let caret = textViewAndCaret ()

match caret with
| None ->
| ValueNone ->
checker.GetProjectOptionsFromScript(
document.FilePath,
sourceText.ToFSharpSourceText(),
Expand All @@ -223,7 +223,7 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
userOpName = userOpName
)

| Some(_, caret) ->
| ValueSome(_, caret) ->
checker.GetProjectOptionsFromScript(
document.FilePath,
sourceText.ToFSharpSourceText(),
Expand Down Expand Up @@ -286,17 +286,22 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
| projectId, fileStamp, parsingOptions, projectOptions, _ ->
let subscription =
match textViewAndCaret () with
| Some(textView, _) ->
subscribeToTextViewEvents (textView, (Some onChangeCaretHandler), (Some onKillFocus), (Some onSetFocus))
| None -> None
| ValueSome(textView, _) ->
subscribeToTextViewEvents (
textView,
(ValueSome onChangeCaretHandler),
(ValueSome onKillFocus),
(ValueSome onSetFocus)
)
| ValueNone -> ValueNone

(projectId, fileStamp, parsingOptions, projectOptions, subscription)

singleFileCache.AddOrUpdate(
document.Id, // The key to the cache
(fun _ value -> addToCacheAndSubscribe value), // Function to add the cached value if the key does not exist
(fun _ _ value -> value), // Function to update the value if the key exists
(document.Project, fileStamp, parsingOptions, projectOptions, None) // The value to add or update
(document.Project, fileStamp, parsingOptions, projectOptions, ValueNone) // The value to add or update
)
|> ignore

Expand All @@ -305,7 +310,7 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
| true, (oldProject, oldFileStamp, parsingOptions, projectOptions, _) ->
if fileStamp <> oldFileStamp || isProjectInvalidated document.Project oldProject ct then
match singleFileCache.TryRemove(document.Id) with
| true, (_, _, _, _, Some subscription) -> subscription.Dispose()
| true, (_, _, _, _, ValueSome subscription) -> subscription.Dispose()
| _ -> ()

return! tryComputeOptionsBySingleScriptOrFile document userOpName
Expand Down Expand Up @@ -521,7 +526,7 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
| true, (_, _, _, projectOptions, subscription) ->
lastSuccessfulCompilations.TryRemove(documentId.ProjectId) |> ignore
checker.ClearCache([ projectOptions ])
subscription |> Option.iter (fun handler -> handler.Dispose())
subscription |> ValueOption.iter (fun handler -> handler.Dispose())
| _ -> ()
}

Expand Down
Loading