/// <summary> /// If there was no $end$ token, place it at the end of the snippet code. Otherwise, it /// defaults to the beginning of the snippet code. /// </summary> private static bool SetEndPositionIfNoneSpecified(IExpansionSession pSession) { if (pSession.GetSnippetNode() is not XElement snippetNode) { return(false); } var ns = snippetNode.Name.NamespaceName; var codeNode = snippetNode.Element(XName.Get("Code", ns)); if (codeNode == null) { return(false); } var delimiterAttribute = codeNode.Attribute("Delimiter"); var delimiter = delimiterAttribute != null ? delimiterAttribute.Value : "$"; if (codeNode.Value.IndexOf(string.Format("{0}end{0}", delimiter), StringComparison.OrdinalIgnoreCase) != -1) { return(false); } var snippetSpan = pSession.GetSnippetSpan(); var newEndSpan = new SnapshotSpan(snippetSpan.End, 0); pSession.EndSpan = newEndSpan; return(true); }
public void OnItemChosen(string title, string pszPath) { var textViewModel = TextView.TextViewModel; if (textViewModel == null) { Debug.Assert(TextView.IsClosed); return; } var textSpan = TextView.Caret.Position.BufferPosition; var expansion = ExpansionServiceProvider.GetExpansionService(TextView); earlyEndExpansionHappened = false; ExpansionSession = expansion.InsertNamedExpansion(title, pszPath, new SnapshotSpan(textSpan, 0), this, LanguageServiceGuid, false); if (earlyEndExpansionHappened) { // EndExpansion was called before InsertNamedExpansion returned, so set // expansionSession to null to indicate that there is no active expansion // session. This can occur when the snippet inserted doesn't have any expansion // fields. ExpansionSession = null; earlyEndExpansionHappened = false; } }
public virtual bool TryInsertExpansion(int startPositionInSubjectBuffer, int endPositionInSubjectBuffer) { var textViewModel = TextView.TextViewModel; if (textViewModel == null) { Debug.Assert(TextView.IsClosed); return(false); } // The expansion itself needs to be created in the data buffer, so map everything up _ = SubjectBuffer.CurrentSnapshot.GetPoint(startPositionInSubjectBuffer); _ = SubjectBuffer.CurrentSnapshot.GetPoint(endPositionInSubjectBuffer); if (!TryGetSpanOnHigherBuffer( SubjectBuffer.CurrentSnapshot.GetSpan(startPositionInSubjectBuffer, endPositionInSubjectBuffer - startPositionInSubjectBuffer), textViewModel.DataBuffer, out var dataBufferSpan)) { return(false); } var expansion = ExpansionServiceProvider.GetExpansionService(TextView); ExpansionSession = expansion.InsertExpansion(dataBufferSpan, this, LanguageServiceGuid); if (ExpansionSession == null) { return(false); } return(true); }
public void EndExpansion() { if (ExpansionSession == null) { earlyEndExpansionHappened = true; } ExpansionSession = null; indentCaretOnCommit = false; }
public virtual bool TryHandleEscape() { if (ExpansionSession != null) { ExpansionSession.EndCurrentExpansion(leaveCaret: true); ExpansionSession = null; return(true); } return(false); }
public virtual bool TryHandleReturn() { if (ExpansionSession != null) { // Only move the caret if the enter was hit within the snippet fields. var hitWithinField = ExpansionSession.GoToNextExpansionField(commitIfLast: false); ExpansionSession.EndCurrentExpansion(leaveCaret: !hitWithinField); ExpansionSession = null; return(hitWithinField); } return(false); }
public virtual bool TryHandleBackTab() { if (ExpansionSession != null) { var tabbedInsideSnippetField = ExpansionSession.GoToPreviousExpansionField(); if (!tabbedInsideSnippetField) { ExpansionSession.EndCurrentExpansion(leaveCaret: true); ExpansionSession = null; } return(tabbedInsideSnippetField); } return(false); }
private void AddReferencesAndImports( IExpansionSession pSession, int position, CancellationToken cancellationToken) { if (!(pSession.GetSnippetNode() is XElement snippetNode)) { return; } var documentWithImports = this.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (documentWithImports == null) { return; } var documentOptions = documentWithImports.GetOptionsAsync(cancellationToken).WaitAndGetResult(cancellationToken); var placeSystemNamespaceFirst = documentOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst); documentWithImports = AddImports(documentWithImports, position, snippetNode, placeSystemNamespaceFirst, cancellationToken); AddReferences(documentWithImports.Project, snippetNode); }
public void OnBeforeInsertion(IExpansionSession pSession) { Logger.Log(FunctionId.Snippet_OnBeforeInsertion); this.ExpansionSession = pSession; }
public void OnAfterInsertion(IExpansionSession pSession) { Logger.Log(FunctionId.Snippet_OnAfterInsertion); }