<commit_before>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Common.Core;
using Microsoft.R.Components.InteractiveWorkflow;
using Microsoft.R.Editor;
using Microsoft.VisualStudio.R.Package.Commands;
using Microsoft.VisualStudio.R.Package.Repl;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;

namespace Microsoft.VisualStudio.R.Package.Help {
    /// <summary>
    /// Base class for 'Help On Current' type of commands.
    /// </summary>
    internal abstract class HelpOnCurrentCommandBase : PackageCommand {
        private const int MaxHelpItemLength = 128;
        private readonly string _baseCommandName;

        protected readonly IRInteractiveWorkflow _workflow;
        protected readonly IActiveWpfTextViewTracker _textViewTracker;

        private IActiveRInteractiveWindowTracker _activeReplTracker;
        public HelpOnCurrentCommandBase(
            Guid group, int id,
            IRInteractiveWorkflow workflow, 
            IActiveWpfTextViewTracker textViewTracker, 
            IActiveRInteractiveWindowTracker activeReplTracker,
            string baseCommandName) :
            base(group, id) {
            _activeReplTracker = activeReplTracker;
            _workflow = workflow;
            _textViewTracker = textViewTracker;
            _baseCommandName = baseCommandName;
        }

        protected override void SetStatus() {
            string item = GetItemUnderCaret();
            if (!string.IsNullOrEmpty(item)) {
                Enabled = true;
                Text = string.Format(CultureInfo.InvariantCulture, _baseCommandName, item);
            } else {
                Enabled = false;
            }
        }

        protected override void Handle() {
            try {
                if (!_workflow.RSession.IsHostRunning) {
                    return;
                }

                // Fetch identifier under the cursor
                string item = GetItemUnderCaret();
                if (item == null || item.Length >= MaxHelpItemLength) {
                    return;
                }

                Handle(item);
            } catch (Exception ex) {
                Debug.Assert(false, string.Format(CultureInfo.InvariantCulture, "Help on current item failed. Exception: {0}", ex.Message));
                // Catch everything so exceptions don't leave the async void method<commit_msg>Limit text length in menu<commit_after>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Common.Core;
using Microsoft.R.Components.InteractiveWorkflow;
using Microsoft.R.Editor;
using Microsoft.VisualStudio.R.Package.Commands;
using Microsoft.VisualStudio.R.Package.Repl;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;

namespace Microsoft.VisualStudio.R.Package.Help {
    /// <summary>
    /// Base class for 'Help On Current' type of commands.
    /// </summary>
    internal abstract class HelpOnCurrentCommandBase : PackageCommand {
        private const int MaxHelpItemLength = 64;
        private readonly string _baseCommandName;

        protected readonly IRInteractiveWorkflow _workflow;
        protected readonly IActiveWpfTextViewTracker _textViewTracker;

        private IActiveRInteractiveWindowTracker _activeReplTracker;
        public HelpOnCurrentCommandBase(
            Guid group, int id,
            IRInteractiveWorkflow workflow, 
            IActiveWpfTextViewTracker textViewTracker, 
            IActiveRInteractiveWindowTracker activeReplTracker,
            string baseCommandName) :
            base(group, id) {
            _activeReplTracker = activeReplTracker;
            _workflow = workflow;
            _textViewTracker = textViewTracker;
            _baseCommandName = baseCommandName;
        }

        protected override void SetStatus() {
            string item = GetItemUnderCaret();
            if (!string.IsNullOrEmpty(item)) {
                Enabled = true;
                if(item.Length >= MaxHelpItemLength) {
                    item = item.Substring(0, MaxHelpItemLength) + (char)0x2026; // Ellipsis
                }
                Text = string.Format(CultureInfo.InvariantCulture, _baseCommandName, item);
            } else {
                Enabled = false;
            }
        }

        protected override void Handle() {
            try {
                if (!_workflow.RSession.IsHostRunning) {
                    return;
                }

                // Fetch identifier under the cursor
                string item = GetItemUnderCaret();
                if (item == null || item.Length >= MaxHelpItemLength) {
                    return;
                }

                Handle(item);
            } catch (Exception ex) {
                Debug.Assert(false, string.Format(CultureInfo.InvariantCulture, "Help on current item failed. Exception: {0}", ex.Message));
                // Catch everything so exceptions don't leave the async void method
{'fraction_non_alphanumeric': 0.07301467820850584, 'fraction_numerical': 0.0024463680843056076, 'mean_word_length': 2.1902761104441777, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7653287', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1240, 'n_words': 346}