Exemplo n.º 1
0
        /// <summary>
        /// Loads JavaScript extensions and preferences from the specified directory.
        /// </summary>
        /// <param name="extensionsDirectory">Pathname of the extensions directory.</param>
        public void LoadExtensions(string extensionsDirectory)
        {
            List <InternalHandle> extensions = new List <InternalHandle>();
            var          files = Directory.EnumerateFiles(extensionsDirectory, "*.*");
            ObjectHandle emmet = _engine.DynamicGlobalObject.window.emmet;

            foreach (string filePath in files)
            {
                if (0 != string.Compare(Path.GetFileName(filePath), PreferencesFileName, true))
                {
                    extensions.Add(_engine.CreateValue(filePath));
                    continue;
                }

                string content   = File.ReadAllText(filePath);
                Handle parameter = _engine.CreateValue(content);
                Handle result    = emmet.Call("loadUserData", emmet, parameter);

                if (result.IsError)
                {
                    this.TraceError($"Failed to load Emmet preferences from {filePath}: {result.AsString}");
                }
                else
                {
                    this.Trace($"Successfully loaded Emmet preferences from {filePath}");
                }
            }

            if (extensions.Count > 0)
            {
                var    parameter = _engine.CreateArray(extensions.ToArray());
                Handle result    = emmet.Call("loadExtensions", emmet, parameter);

                if (result.IsError)
                {
                    this.TraceError($"Failed to load Emmet extensions: {result.AsString}");
                }
                else
                {
                    this.Trace($"Successfully loaded {extensions.Count} Emmet extensions");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Looks for tab stops in the specified content and returns a processed version with expanded
        /// placeholders and tab stops found.
        /// </summary>
        /// <param name="engine">V8 instance with Emmet engine compiled in it.</param>
        /// <param name="content">Expanded abbreviation content.</param>
        /// <exception cref="Exception{EmmetEngineExceptionArgs}">
        /// Indicates that Emmet engine has failed to parse the specified content.
        /// </exception>
        public static TabStopsParser ParseContent(V8Engine engine, string content)
        {
            ObjectHandle tabStopsUtil  = engine.DynamicGlobalObject.window.emmet.tabStops;
            Handle       extractResult = tabStopsUtil.Call("extract", null, engine.CreateValue(content));

            if (extractResult.IsError)
            {
                var ex = new EmmetEngineExceptionArgs(
                    "Error while trying to extract tab stops.",
                    extractResult);
                throw new Exception <EmmetEngineExceptionArgs>(ex);
            }

            TabStopsParser retVal      = new TabStopsParser();
            ObjectHandle   tabStopsObj = (ObjectHandle)extractResult;

            retVal.Content = tabStopsObj.GetProperty(@"text").AsString;
            ObjectHandle tabStopsList = tabStopsObj.GetProperty(@"tabstops");

            // Tab stops should be added before modifying document so that editor can track their position.
            int tabStopsCount = tabStopsList.ArrayLength;

            if (tabStopsCount > 0)
            {
                retVal.TabStops      = new Range[tabStopsCount];
                retVal.TabStopGroups = new int[tabStopsCount];

                for (int i = 0; i < tabStopsCount; i++)
                {
                    ObjectHandle tabStopObj = tabStopsList.GetProperty(i.ToString());
                    int          start      = tabStopObj.GetProperty("start").AsInt32;
                    int          end        = tabStopObj.GetProperty("end").AsInt32;
                    int          group      = tabStopObj.GetProperty("group").AsInt32;

                    retVal.TabStops[i]      = new Range(start, end);
                    retVal.TabStopGroups[i] = group;
                }
            }

            return(retVal);
        }