Пример #1
0
        /// <summary>
        /// Executes Emmet command with the specified identifier on the specified editor view.
        /// </summary>
        /// <param name="cmdId">Identifier of the command to execute.</param>
        /// <param name="view">Editor to execute command in.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Indicates that the specified command identifier was not found.
        /// </exception>
        public bool RunCommand(int cmdId, ICodeEditor view)
        {
            if (!_initialized)
            {
                InitializeEngine();
            }

            string script;

            switch (cmdId)
            {
            case PackageIds.CmdIDExpandAbbreviation:
                script = GetExpandAbbreviationScript(view);
                break;

            case PackageIds.CmdIDWrapWithAbbreviation:
                script = GetWrapWithAbbreviationScript(view);
                if (script is null)
                {
                    return(false);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(cmdId),
                          cmdId,
                          "Specified command identifier not found.");
            }

            Trace($"Running script: {script}");
            JavaScriptContext.Current = _context;
            JavaScriptValue result = JavaScriptContext.RunScript(script, _currentSourceContext++);

            if (result.ValueType is JavaScriptValueType.Boolean)
            {
                Trace("Emmet engine failed to execute command.");
                return(false);
            }

            string replacement = result.ToString();

            JavaScriptContext.Idle();
            if (cmdId is PackageIds.CmdIDExpandAbbreviation)
            {
                view.ReplaceCurrentLine(replacement);
            }
            else
            {
                view.ReplaceSelection(replacement);
            }

            Trace($"Command {script} completed successfully.");
            return(true);
        }