public ContainedDocument( IThreadingContext threadingContext, DocumentId documentId, ITextBuffer subjectBuffer, ITextBuffer dataBuffer, IVsTextBufferCoordinator bufferCoordinator, Workspace workspace, VisualStudioProject project, IComponentModel componentModel, AbstractFormattingRule vbHelperFormattingRule) : base(threadingContext) { _componentModel = componentModel; _workspace = workspace; _project = project; Id = documentId; SubjectBuffer = subjectBuffer; DataBuffer = dataBuffer; BufferCoordinator = bufferCoordinator; _differenceSelectorService = componentModel.GetService <ITextDifferencingSelectorService>(); _snapshotTracker = new ReiteratedVersionSnapshotTracker(SubjectBuffer); _vbHelperFormattingRule = vbHelperFormattingRule; _hostType = GetHostType(); s_containedDocuments.TryAdd(documentId, this); }
private void AdjustIndentationForSpan( Document document, ITextEdit edit, TextSpan visibleSpan, AbstractFormattingRule baseIndentationRule, OptionSet options) { var root = document.GetSyntaxRootSynchronously(CancellationToken.None); using var rulePool = SharedPools.Default <List <AbstractFormattingRule> >().GetPooledObject(); using var spanPool = SharedPools.Default <List <TextSpan> >().GetPooledObject(); var venusFormattingRules = rulePool.Object; var visibleSpans = spanPool.Object; venusFormattingRules.Add(baseIndentationRule); venusFormattingRules.Add(ContainedDocumentPreserveFormattingRule.Instance); var formattingRules = venusFormattingRules.Concat(Formatter.GetDefaultFormattingRules(document)); var workspace = document.Project.Solution.Workspace; var changes = Formatter.GetFormattedTextChanges( root, new TextSpan[] { CommonFormattingHelpers.GetFormattingSpan(root, visibleSpan) }, workspace, options, formattingRules, CancellationToken.None); visibleSpans.Add(visibleSpan); var newChanges = FilterTextChanges(document.GetTextSynchronously(CancellationToken.None), visibleSpans, changes.ToReadOnlyCollection()).Where(t => visibleSpan.Contains(t.Span)); foreach (var change in newChanges) { edit.Replace(change.Span.ToSpan(), change.NewText); } }
protected override ISmartTokenFormatter CreateSmartTokenFormatter( CompilationUnitSyntax root, TextLine lineToBeIndented, IndentationOptions options, AbstractFormattingRule baseIndentationRule) { var rules = ImmutableArray.Create(baseIndentationRule).AddRange(CSharpSyntaxFormatting.Instance.GetDefaultFormattingRules()); return(new CSharpSmartTokenFormatter(options, rules, root)); }
internal ContainedLanguage( IVsTextBufferCoordinator bufferCoordinator, IComponentModel componentModel, VisualStudioProject project, IVsHierarchy hierarchy, uint itemid, VisualStudioProjectTracker projectTrackerOpt, ProjectId projectId, TLanguageService languageService, AbstractFormattingRule vbHelperFormattingRule = null) { this.BufferCoordinator = bufferCoordinator; this.ComponentModel = componentModel; this.Project = project; _languageService = languageService; this.Workspace = projectTrackerOpt?.Workspace ?? componentModel.GetService <VisualStudioWorkspace>(); _editorAdaptersFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>(); _diagnosticAnalyzerService = componentModel.GetService <IDiagnosticAnalyzerService>(); // Get the ITextBuffer for the secondary buffer Marshal.ThrowExceptionForHR(bufferCoordinator.GetSecondaryBuffer(out var secondaryTextLines)); var secondaryVsTextBuffer = (IVsTextBuffer)secondaryTextLines; SubjectBuffer = _editorAdaptersFactoryService.GetDocumentBuffer(secondaryVsTextBuffer); // Get the ITextBuffer for the primary buffer Marshal.ThrowExceptionForHR(bufferCoordinator.GetPrimaryBuffer(out var primaryTextLines)); DataBuffer = _editorAdaptersFactoryService.GetDataBuffer((IVsTextBuffer)primaryTextLines); // Create our tagger var bufferTagAggregatorFactory = ComponentModel.GetService <IBufferTagAggregatorFactoryService>(); _bufferTagAggregator = bufferTagAggregatorFactory.CreateTagAggregator <ITag>(SubjectBuffer); if (!ErrorHandler.Succeeded(((IVsProject)hierarchy).GetMkDocument(itemid, out var filePath))) { // we couldn't look up the document moniker from an hierarchy for an itemid. // Since we only use this moniker as a key, we could fall back to something else, like the document name. Debug.Assert(false, "Could not get the document moniker for an item from its hierarchy."); if (!hierarchy.TryGetItemName(itemid, out filePath)) { FatalError.Report(new System.Exception("Failed to get document moniker for a contained document")); } } DocumentId documentId; if (this.Project != null) { documentId = this.Project.AddSourceTextContainer( SubjectBuffer.AsTextContainer(), filePath, sourceCodeKind: SourceCodeKind.Regular, folders: default,
public static Tuple <string, string, VsTextSpan> EnsureEventHandler( Document thisDocument, Document targetDocument, string className, string objectName, string objectTypeName, string nameOfEvent, string eventHandlerName, uint itemidInsertionPoint, bool useHandlesClause, AbstractFormattingRule additionalFormattingRule, IGlobalOptionService globalOptions, CancellationToken cancellationToken) #pragma warning restore IDE0060 // Remove unused parameter { var thisCompilation = thisDocument.Project.GetCompilationAsync(cancellationToken).WaitAndGetResult_Venus(cancellationToken); var type = thisCompilation.GetTypeByMetadataName(className); var existingEventHandlers = GetCompatibleEventHandlers(targetDocument, className, objectTypeName, nameOfEvent, cancellationToken); var existingHandler = existingEventHandlers.SingleOrDefault(e => e.Item1 == eventHandlerName); if (existingHandler != null) { return(Tuple.Create(existingHandler.Item2, (string)null, default(VsTextSpan))); } // Okay, it doesn't exist yet. Let's create it. var codeGenerationService = targetDocument.GetLanguageService <ICodeGenerationService>(); var syntaxFactory = targetDocument.GetLanguageService <SyntaxGenerator>(); var eventMember = GetEventSymbol(thisDocument, objectTypeName, nameOfEvent, type, cancellationToken); if (eventMember == null) { throw new InvalidOperationException(); } var eventType = ((IEventSymbol)eventMember).Type; if (eventType.Kind != SymbolKind.NamedType || ((INamedTypeSymbol)eventType).DelegateInvokeMethod == null) { throw new InvalidOperationException(ServicesVSResources.Event_type_is_invalid); } var handlesExpressions = useHandlesClause ? ImmutableArray.Create(syntaxFactory.MemberAccessExpression( objectName != null ? syntaxFactory.IdentifierName(objectName) : syntaxFactory.ThisExpression(), syntaxFactory.IdentifierName(nameOfEvent))) : default; var invokeMethod = ((INamedTypeSymbol)eventType).DelegateInvokeMethod; var newMethod = CodeGenerationSymbolFactory.CreateMethodSymbol( attributes: default,
public static IEnumerable <AbstractFormattingRule> Concat(this AbstractFormattingRule rule, IEnumerable <AbstractFormattingRule> rules) => SpecializedCollections.SingletonEnumerable(rule).Concat(rules);
protected abstract ISmartTokenFormatter CreateSmartTokenFormatter( TSyntaxRoot root, TextLine lineToBeIndented, IndentationOptions options, AbstractFormattingRule baseFormattingRule);