private MemberNode GetLibraries(ITextBuffer textBuffer) { JsonEditorDocument document = JsonEditorDocument.FromTextBuffer(textBuffer); if (document != null) { Node topLevelNode = document.DocumentNode.TopLevelValue.FindType <ObjectNode>(); SortedNodeList <Node> topLevelNodeChildren = JsonHelpers.GetChildren(topLevelNode); IEnumerable <MemberNode> jsonMembers = topLevelNodeChildren.OfType <MemberNode>(); return(jsonMembers.FirstOrDefault(m => m.UnquotedNameText == ManifestConstants.Libraries)); } return(null); }
private void InsertIntoTextBuffer(IVsTextBuffer document, LibraryInstallationState libraryInstallationState, Manifest manifest) { ITextBuffer textBuffer = GetTextBuffer(document); if (textBuffer != null) { MemberNode libraries = GetLibraries(textBuffer); SortedNodeList <Node> children = JsonHelpers.GetChildren(libraries); if (children.Count > 0) { ArrayNode arrayNode = children.OfType <ArrayNode>().First(); string newLibrary = GetLibraryTextToBeInserted(libraryInstallationState, manifest); bool containsLibrary = arrayNode.BlockChildren.Any(); int insertionIndex = libraries.End - 1; string lineBreakText = GetLineBreakTextFromPreviousLine(textBuffer, insertionIndex); string insertionText; if (containsLibrary) { insertionText = "," + lineBreakText + newLibrary + lineBreakText; } else { insertionText = newLibrary + lineBreakText; } if (insertionIndex > 0) { FormatSelection(textBuffer, insertionIndex, insertionText); } } } }
public override void Invoke(CancellationToken cancellationToken) { Telemetry.TrackUserTask("Invoke-UpdateSuggestedAction"); if (_disabled) { return; } try { IDependencies dependencies = _provider.DependenciesFactory.FromConfigFile(_provider.ConfigFilePath); IProvider provider = dependencies.GetProvider(_provider.InstallationState.ProviderId); ILibraryCatalog catalog = provider?.GetCatalog(); if (catalog == null) { return; } SortedNodeList <Node> children = JsonHelpers.GetChildren(_provider.LibraryObject); MemberNode member = children.OfType <MemberNode>().FirstOrDefault(m => m.UnquotedNameText == ManifestConstants.Library); if (member != null) { using (ITextEdit edit = TextBuffer.CreateEdit()) { edit.Replace(new Span(member.Value.Start, member.Value.Width), "\"" + _updatedLibraryId + "\""); edit.Apply(); } } } catch (Exception ex) { Logger.LogEvent(ex.ToString(), LogLevel.Error); Telemetry.TrackException("UpdateSuggestedActionFailed", ex); } }
public static bool TryGetInstallationState(ObjectNode parent, out ILibraryInstallationState installationState, string defaultProvider = null) { installationState = null; if (parent == null) { return(false); } var state = new LibraryInstallationStateOnDisk(); SortedNodeList <Node> children = GetChildren(parent); foreach (MemberNode child in children.OfType <MemberNode>()) { switch (child.UnquotedNameText) { case ManifestConstants.Provider: state.ProviderId = child.UnquotedValueText; break; case ManifestConstants.Library: state.LibraryId = child.UnquotedValueText; break; case ManifestConstants.Destination: state.DestinationPath = child.UnquotedValueText; break; case ManifestConstants.Files: state.Files = (child.Value as ArrayNode)?.Elements.Select(e => e.UnquotedValueText).ToList(); break; } } children = GetChildren(parent.Parent?.FindType <ObjectNode>()); IEnumerable <MemberNode> rootMembers = children?.OfType <MemberNode>(); // Check for defaultProvider if (string.IsNullOrEmpty(state.ProviderId)) { if (rootMembers != null) { foreach (MemberNode child in rootMembers) { if (child.UnquotedNameText == "defaultProvider") { state.ProviderId = child.UnquotedValueText; } } } } // Check for defaultDestination if (string.IsNullOrEmpty(state.DestinationPath)) { if (rootMembers != null) { foreach (MemberNode child in rootMembers) { if (child.UnquotedNameText == ManifestConstants.DefaultDestination) { state.DestinationPath = child.UnquotedValueText; } } } } var converter = new LibraryStateToFileConverter(defaultProvider, defaultDestination: null); installationState = converter.ConvertToLibraryInstallationState(state); return(!string.IsNullOrEmpty(installationState.ProviderId)); }
public static bool TryGetInstallationState(ObjectNode parent, out ILibraryInstallationState installationState, string defaultProvider = null) { installationState = null; if (parent == null) { return(false); } var state = new LibraryInstallationStateOnDisk(); SortedNodeList <Node> children = GetChildren(parent); string GetCanonicalizedValue(BlockChildNode m) { if (m.Value is TokenNode value) { return(value.GetCanonicalizedText()); } return(m.UnquotedValueText); } foreach (MemberNode child in children.OfType <MemberNode>()) { // note: the Json parser escapes backslashes in the node's Value text, // so we need to unescape those so that they match the value from the manifest. switch (child.UnquotedNameText) { case ManifestConstants.Provider: state.ProviderId = GetCanonicalizedValue(child); break; case ManifestConstants.Library: state.LibraryId = GetCanonicalizedValue(child); break; case ManifestConstants.Destination: state.DestinationPath = GetCanonicalizedValue(child); break; case ManifestConstants.Files: state.Files = (child.Value as ArrayNode)?.Elements.Select(e => GetCanonicalizedValue(e)).ToList(); break; } } children = GetChildren(parent.Parent?.FindType <ObjectNode>()); IEnumerable <MemberNode> rootMembers = children?.OfType <MemberNode>(); // Check for defaultProvider if (string.IsNullOrEmpty(state.ProviderId)) { if (rootMembers != null) { foreach (MemberNode child in rootMembers) { if (child.UnquotedNameText == "defaultProvider") { state.ProviderId = child.UnquotedValueText; } } } } // Check for defaultDestination if (string.IsNullOrEmpty(state.DestinationPath)) { if (rootMembers != null) { foreach (MemberNode child in rootMembers) { if (child.UnquotedNameText == ManifestConstants.DefaultDestination) { state.DestinationPath = child.UnquotedValueText; } } } } var converter = new LibraryStateToFileConverter(defaultProvider, defaultDestination: null); installationState = converter.ConvertToLibraryInstallationState(state); return(!string.IsNullOrEmpty(installationState.ProviderId)); }