/// <summary>
        /// Attaches this instance to the given code editor.
        /// </summary>
        /// <param name="editor">Editor to attach to.</param>
        public bool Attach(IEmmetEditor editor)
        {
            _editor = editor;

            // Detect syntax at the caret position
            string contentType = editor.GetContentTypeInActiveBuffer();
            if (string.IsNullOrEmpty(contentType))
                return false;

            // css, less or scss
            if (contentType.EndsWith(@"ss"))
            {
                _syntax = contentType;
            }
            else if (contentType == @"htmlx" || contentType.StartsWith("razor"))
            {
                _syntax = @"html";
            }
            else
            {
                this.TraceWarning($"Syntax {contentType} is not supported");
                return false;
            }

            return true;
        }
        /// <summary>
        /// Attaches this instance to the given code editor.
        /// </summary>
        /// <param name="editor">Editor to attach to.</param>
        public bool Attach(IEmmetEditor editor)
        {
            _editor = editor;

            // Detect syntax at the caret position
            string contentType = editor.GetContentTypeInActiveBuffer();

            if (string.IsNullOrEmpty(contentType))
            {
                return(false);
            }

            // css, less or scss
            if (contentType.EndsWith(@"ss"))
            {
                _syntax = contentType;
            }
            else if (contentType == @"htmlx" || contentType.StartsWith("razor"))
            {
                _syntax = @"html";
            }
            else
            {
                this.TraceWarning($"Syntax {contentType} is not supported");
                return(false);
            }

            return(true);
        }
Esempio n. 3
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="editor">Editor to execute command in.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Indicates that the specified command identifier was not found.
        /// </exception>
        public bool RunCommand(int cmdId, IEmmetEditor editor)
        {
            if (null == _engine)
            {
                InitializeEngine();
            }

            if (!_editor.Attach(editor))
            {
                return(false);
            }

            string script = string.Empty;

            switch (cmdId)
            {
            case PackageIds.CmdIDExpandAbbreviation:
                script = "window.emmet.run('expand_abbreviation', editor);";
                break;

            case PackageIds.CmdIDWrapWithAbbreviation:
                script = "window.emmet.run('wrap_with_abbreviation', editor);";
                break;

            case PackageIds.CmdIDToggleComment:
                script = "window.emmet.run('toggle_comment', editor);";
                break;

            case PackageIds.CmdIDMergeLines:
                script = "window.emmet.run('merge_lines', editor);";
                break;

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

            Handle retVal = _engine.Execute(script);

            _editor.Detach();

            if (retVal.IsError)
            {
                this.TraceError($"Emmet engine error: {retVal.AsString}");
                return(false);
            }
            else if (retVal.AsBoolean == false)
            {
                this.Trace($"Command {script} returned false.");
                return(false);
            }

            this.Trace($"Command {script} completed successfully.");
            return(true);
        }
Esempio n. 4
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="editor">Editor to execute command in.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Indicates that the specified command identifier was not found.
        /// </exception>
        public bool RunCommand(int cmdId, IEmmetEditor editor)
        {
            if (null == _engine)
                InitializeEngine();

            if (!_editor.Attach(editor))
                return false;

            string script = string.Empty;
            switch (cmdId)
            {
                case PackageIds.CmdIDExpandAbbreviation:
                    script = "window.emmet.run('expand_abbreviation', editor);";
                    break;
                case PackageIds.CmdIDWrapWithAbbreviation:
                    script = "window.emmet.run('wrap_with_abbreviation', editor);";
                    break;
                case PackageIds.CmdIDToggleComment:
                    script = "window.emmet.run('toggle_comment', editor);";
                    break;
                case PackageIds.CmdIDMergeLines:
                    script = "window.emmet.run('merge_lines', editor);";
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(cmdId),
                                                          cmdId,
                                                          "Specified command identifier not found.");
            }

            Handle retVal = _engine.Execute(script);
            _editor.Detach();

            if (retVal.IsError)
            {
                this.TraceError($"Emmet engine error: {retVal.AsString}");
                return false;
            }
            else if (retVal.AsBoolean == false)
            {
                this.Trace($"Command {script} returned false.");
                return false;
            }

            this.Trace($"Command {script} completed successfully.");
            return true;
        }
Esempio n. 5
0
        public void RunCommand_ExpandsCssAbbreviation()
        {
            // Arrange
            string       result = string.Empty;
            IEmmetEditor editor = Substitute.For <IEmmetEditor>();

            editor.GetContentTypeInActiveBuffer().Returns("css");
            editor.GetContent().Returns("p10");
            editor.GetSelectionRange().Returns(new Range(3, 3));
            editor.GetCurrentLineRange().Returns(new Range(0, 3));
            editor.GetCaretPosition().Returns(3);
            editor.ReplaceContentRange(Arg.Do <string>(s => result = s), Arg.Any <int>(), Arg.Any <int>());

            // Act
            _engine.RunCommand(PackageIds.CmdIDExpandAbbreviation, editor).Should().BeTrue();
            result.Should().Be("padding: 10px;");
        }
 /// <summary>
 /// Detaches this instance from the editor window, cleans up any internal caches.
 /// </summary>
 public void Detach()
 {
     _editor = null;
     _syntax = null;
 }
 /// <summary>
 /// Detaches this instance from the editor window, cleans up any internal caches.
 /// </summary>
 public void Detach()
 {
     _editor = null;
     _syntax = null;
 }