Example #1
0
        /// <summary>
        /// Inserts a final new line for the given <see cref="ITextBuffer"/> based on
        /// whether the option to insert it is enabled in the current set of <see cref="IEditorOptions"/> applicable to the buffer
        /// </summary>
        /// <param name="buffer">The <see cref="ITextBuffer"/> in which the final new line has to be inserted in</param>
        /// <param name="editorOptions">The current set of <see cref="IEditorOptions"/> applicable to the buffer</param>
        /// <returns>Whether the operation on the buffer succeded or not</returns>
        public static bool TryInsertFinalNewLine(ITextBuffer buffer, IEditorOptions editorOptions)
        {
            var           currentSnapshot = buffer.CurrentSnapshot;
            var           lineCount       = currentSnapshot.LineCount;
            var           lastLine        = currentSnapshot.GetLineFromLineNumber(lineCount - 1);
            ITextSnapshot changedSnapshot = null;

            if (lastLine.Start.Position != lastLine.EndIncludingLineBreak.Position) // Check if final new line is not already present
            {
                var IsTrimTrailingWhitespacesSetExplicitlyToFalse = editorOptions.IsOptionDefined <bool>(DefaultOptions.TrimTrailingWhiteSpaceOptionId, true) && !editorOptions.GetOptionValue <bool>(DefaultOptions.TrimTrailingWhiteSpaceOptionId);

                if (!IsTrimTrailingWhitespacesSetExplicitlyToFalse && !HasAnyNonWhitespaceCharacters(lastLine)) // Last Line contains only of whitespace and trim trailing whitespaces is set to false
                {
                    var spanToDelete = lastLine.ExtentIncludingLineBreak;
                    changedSnapshot = buffer.Delete(spanToDelete);
                }
                else  // Non empty last line or empty last line with trim trailing whitespaces set to false. Insert a new line after the current line
                {
                    string lineBreakToInsert       = GetNewLineCharacterToInsert(lastLine, editorOptions);
                    var    positionToInsertNewLine = lastLine.End.Position;
                    changedSnapshot = buffer.Insert(positionToInsertNewLine, lineBreakToInsert);
                }
                // Edits were successfull
                if (changedSnapshot != null && currentSnapshot != changedSnapshot)
                {
                    return(true);
                }
                return(false);
            }
            return(true);
        }
Example #2
0
        public bool SaveOption(IEditorOptions options, string optionName)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            while (true)
            {
                if (options.IsOptionDefined(optionName, true))
                {
                    break;
                }

                options = options.Parent;
                if (options == null)
                {
                    return(false);       //Unable to save option.
                }
            }

            IVsSettingsManager       manager = this.SettingsManagerService;
            IVsWritableSettingsStore store;

            Marshal.ThrowExceptionForHR(manager.GetWritableSettingsStore((uint)__VsSettingsScope.SettingsScope_UserSettings, out store));

            string result = options.GetOptionValue(optionName).ToString();

            Marshal.ThrowExceptionForHR(store.SetString("Text Editor", optionName, result));

            return(true);
        }
Example #3
0
        internal OutliningManager(ITextBuffer editBuffer, ITagAggregator <IOutliningRegionTag> tagAggregator, IEditorOptions options)
        {
            this.editBuffer    = editBuffer;
            this.tagAggregator = tagAggregator as IAccurateTagAggregator <IOutliningRegionTag>;

            bool keepTrackingCurrent = false;

            if (options != null && options.IsOptionDefined("Stress Test Mode", false))
            {
                keepTrackingCurrent = options.GetOptionValue <bool>("Stress Test Mode");
            }
            collapsedRegionTree = new TrackingSpanTree <Collapsed>(editBuffer, keepTrackingCurrent);

            tagAggregator.BatchedTagsChanged += OutliningRegionTagsChanged;
            this.editBuffer.Changed          += SourceTextChanged;
        }
Example #4
0
        protected override void Initialize()
        {
            base.Initialize();

            // Log telemetry on the initial settings values
            OptionsPagePackage.TelemetrySession = TelemetrySessionForPPT.Create(this.GetType().Assembly);

            IEditorOptions globalOptions = (Common.GetMefService <IEditorOptionsFactoryService>()).GlobalOptions;

            foreach (object option in OptionsPage.Options)
            {
                var definition = option as LabeledOptionDefinition;
                if (definition != null)
                {
                    if (globalOptions.IsOptionDefined(definition.Name, localScopeOnly: true))
                    {
                        var value = globalOptions.GetOptionValue <bool>(definition.Name);
                        OptionsPagePackage.TelemetrySession.PostEvent("VS/PPT-Options/OptionInitialValue", definition.Name, value);
                    }
                }
            }
        }
Example #5
0
        public OptionsControl(IEditorOptions globalOptions, IEnumerable <LabeledOptionDefinitionGroup> optionGroups)
        {
            InitializeComponent();

            _globalOptions = globalOptions;

            foreach (var optionGroup in optionGroups)
            {
                GroupBox   groupBox = null;
                StackPanel itemList = null;

                // Create a group if the group heading is not null or empty.
                if (!string.IsNullOrEmpty(optionGroup.GroupHeading))
                {
                    groupBox        = new GroupBox();
                    groupBox.Header = optionGroup.GroupHeading;

                    itemList             = new StackPanel();
                    itemList.Orientation = Orientation.Vertical;

                    groupBox.Content = itemList;

                    groupBox.Padding    = new Thickness(4.0, 6.0, 4.0, 2.0);
                    groupBox.Margin     = new Thickness(0.0, 4.0, 0.0, 0.0);
                    groupBox.Foreground = SystemColors.WindowTextBrush;
                }

                bool atLeastOneOptionIsDefined = false;
                foreach (var labeledOption in optionGroup.LabeledOptionDefinitions)
                {
                    if (_globalOptions.IsOptionDefined(labeledOption.Name, localScopeOnly: true))
                    {
                        // Add a checkbox to the group if it exists; if the group doesn't exist, then
                        // add the checkbox to the list of options.
                        CheckBox box = new CheckBox();
                        _allOptions.Add(box);

                        box.IsEnabled = true;
                        box.IsChecked = _globalOptions.GetOptionValue <bool>(labeledOption.Name);

                        box.Tag     = labeledOption;
                        box.Content = labeledOption.Label;

                        box.Margin = new Thickness(0.0, 1.0, 0.0, 1.0);

                        // We found at least one option that can is defined in global options.
                        // Now we can add the group to the options page (if there is a group).
                        if (!atLeastOneOptionIsDefined)
                        {
                            if (groupBox != null)
                            {
                                this.Options.Items.Add(groupBox);
                            }

                            atLeastOneOptionIsDefined = true;
                        }

                        if (itemList != null)
                        {
                            itemList.Children.Add(box);
                        }
                        else
                        {
                            this.Options.Items.Add(box);
                        }
                    }
                }
            }
        }
Example #6
0
        public OptionsControl(IEditorOptions globalOptions, IEnumerable<LabeledOptionDefinitionGroup> optionGroups)
        {
            InitializeComponent();

            _globalOptions = globalOptions;

            foreach (var optionGroup in optionGroups)
            {
                GroupBox groupBox = null;
                StackPanel itemList = null;

                // Create a group if the group heading is not null or empty.
                if (!string.IsNullOrEmpty(optionGroup.GroupHeading))
                {
                    groupBox = new GroupBox();
                    groupBox.Header = optionGroup.GroupHeading;

                    itemList = new StackPanel();
                    itemList.Orientation = Orientation.Vertical;

                    groupBox.Content = itemList;

                    groupBox.Padding = new Thickness(4.0, 6.0, 4.0, 2.0);
                    groupBox.Margin = new Thickness(0.0, 4.0, 0.0, 0.0);
                    groupBox.Foreground = SystemColors.WindowTextBrush;
                }

                bool atLeastOneOptionIsDefined = false;
                foreach (var labeledOption in optionGroup.LabeledOptionDefinitions)
                {
                    if (_globalOptions.IsOptionDefined(labeledOption.Name, localScopeOnly: true))
                    {
                        // Add a checkbox to the group if it exists; if the group doesn't exist, then
                        // add the checkbox to the list of options.
                        CheckBox box = new CheckBox();
                        _allOptions.Add(box);

                        box.IsEnabled = true;
                        box.IsChecked = _globalOptions.GetOptionValue<bool>(labeledOption.Name);

                        box.Tag = labeledOption;
                        box.Content = labeledOption.Label;

                        box.Margin = new Thickness(0.0, 1.0, 0.0, 1.0);

                        // We found at least one option that can is defined in global options.
                        // Now we can add the group to the options page (if there is a group).
                        if (!atLeastOneOptionIsDefined)
                        {
                            if (groupBox != null)
                            {
                                this.Options.Items.Add(groupBox);
                            }

                            atLeastOneOptionIsDefined = true;
                        }

                        if (itemList != null)
                        {
                            itemList.Children.Add(box);
                        }
                        else
                        {
                            this.Options.Items.Add(box);
                        }
                    }
                }
            }
        }
Example #7
0
 public static T GetOptionValue <T>(this IEditorOptions options, string optionId, T defaultValue)
 {
     return(options.IsOptionDefined(optionId, false) ?
            options.GetOptionValue <T>(optionId) : defaultValue);
 }