예제 #1
0
        internal static string GetSignatureHelp(string scriptFile, int caret)
        {
            Output.WriteLine("GetSignatureHelp");

            var script = new SourceInfo(scriptFile);

            string result = null;

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            if (!script.RawFile.EndsWith(".g.cs"))
            {
                CSScriptHelper.DecorateIfRequired(ref script.Content, ref caret);
            }

            Project project = CSScriptHelper.GenerateProjectFor(script);
            var     sources = project.Files
                              .Where(f => f != project.Script)
                              .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                              .ToArray();

            string bestMatchIndex;
            var    items = Autocompleter.GetMethodSignatures(script.Content, caret, out bestMatchIndex, project.Refs, sources);

            result = $"{bestMatchIndex}\r\n" + items.Select(x => x.EscapeLB())
                     .JoinSerializedLines();

            return(result);
        }
예제 #2
0
        internal static string FindUsings(string scriptFile, string word, bool rich_serialization)
        {
            Output.WriteLine("FindUsings");

            var script = new SourceInfo(scriptFile);

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            Project project = CSScriptHelper.GenerateProjectFor(script);
            var     sources = project.Files
                              .Where(f => f != project.Script)
                              .Select(f => new Tuple <string, string>(File.ReadAllText(f), f));

            var regions = Autocompleter.GetNamespacesFor(script.Content, word, project.Refs, sources);

            fullyLoaded = true;

            if (rich_serialization)
            {
                return(regions.Select(Intellisense.Common.TypeInfo.Serialize).JoinSerializedLines());
            }
            else
            {
                return(regions.Select(x => x.Namespace).JoinBy("\n"));
            }
        }
예제 #3
0
        internal static string LookopDirectivePath(SourceInfo script, int offset, string directive, string word, string extensions = null)
        {
            extensions = extensions ?? GetAssociatedCompletionContext(directive);

            if (extensions != null && word.HasText()) // file of the //css_inc or //css_ref directive
            {
                bool ignoreCase = Utils.IsWinows;

                var probing_dirs = CSScriptHelper.GenerateProjectFor(script)
                                   .SearchDirs;

                var match = probing_dirs.Select(dir =>
                {
                    if (Directory.Exists(dir))
                    {
                        return(Directory.GetFiles(dir, "*")
                               .FirstOrDefault(file =>
                        {
                            return Path.GetFileName(file).IsSameAs(word, ignoreCase)
                            ||
                            (Path.GetFileNameWithoutExtension(file).IsSameAs(word, ignoreCase) &&
                             extensions.Contains(Path.GetExtension(file)));
                        }));
                    }
                    else
                    {
                        return(null);
                    }
                })
                            .FirstOrDefault(x => x != null);

                return(match);
            }
            return(null);
        }
예제 #4
0
        internal static DomRegion ResolveRaw(string scriptFile, int offset)
        {
            var script = new SourceInfo(scriptFile);

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            DomRegion region = DomRegion.Empty;

            ParseAsCssDirective(script.Content, offset,
                                directive =>
            {
                region = CssSyntax.Resolve(directive);
            },
                                (directive, arg, extensions) =>
            {
                if (LookopDirectivePath(script, offset, directive, arg) is string file)
                {
                    region = new DomRegion
                    {
                        BeginColumn = -1,
                        BeginLine   = -1,
                        EndLine     = -1,
                        FileName    = file,
                        IsEmpty     = false
                    }
                }
                ;
                else
                {
                    region = CssSyntax.Resolve(directive);
                }
            });

            if (region.IsEmpty)
            {
                bool decorated = false;
                if (!script.RawFile.EndsWith(".g.cs"))
                {
                    decorated = CSScriptHelper.DecorateIfRequired(ref script.Content, ref offset);
                }

                Project project = CSScriptHelper.GenerateProjectFor(script);
                var     sources = project.Files
                                  .Where(f => f != project.Script)
                                  .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                                  .ToArray();

                region = Autocompleter.ResolveSymbol(script.Content, offset, script.File, project.Refs, sources);
                if (decorated && region.FileName == script.File)
                {
                    CSScriptHelper.Undecorate(script.Content, ref region);
                }
            }

            return(region);
        }
예제 #5
0
 internal static string[] LookupDirectivePaths(SourceInfo script, int offset, string directive, string word, string extensions)
 {
     if (extensions != null)
     {
         return(CSScriptHelper.GenerateProjectFor(script)
                .SearchDirs
                .Where(dir => Directory.Exists(dir))
                .SelectMany(dir => extensions.Split('|').Select(x => new { ProbingDir = dir, FileExtension = x }))
                .SelectMany(x => Directory.GetFiles(x.ProbingDir, "*" + x.FileExtension))
                .Where(x => x != script.File)
                .ToArray());
     }
     return(new string[0]);
 }
예제 #6
0
        internal static string FindRefreneces(string scriptFile, int offset, string context)
        {
            Output.WriteLine("FindRefreneces");

            var script = new SourceInfo(scriptFile);

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            bool decorated = false;

            if (!script.RawFile.EndsWith(".g.cs"))
            {
                decorated = CSScriptHelper.DecorateIfRequired(ref script.Content, ref offset);
            }

            Project project = CSScriptHelper.GenerateProjectFor(script);
            var     sources = project.Files
                              .Where(f => f != project.Script)
                              .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                              .ToArray();

            var regions = new List <string>();

            if (context == "all")  // include definition and constructors
            {
                DomRegion[] refs = Autocompleter.GetSymbolSourceRefs(script.Content, offset, script.File, project.Refs, sources);
                foreach (DomRegion item in refs)
                {
                    DomRegion region = item;
                    if (decorated && item.FileName == script.File)
                    {
                        CSScriptHelper.Undecorate(script.Content, ref region);
                    }

                    regions.Add($"{item.FileName}({region.BeginLine},{item.BeginColumn}): ...");
                }
            }

            regions.AddRange(Autocompleter.FindReferences(script.Content, offset, script.File, project.Refs, sources));

            fullyLoaded = true;

            return(regions.Distinct().JoinBy("\n"));
        }
예제 #7
0
        static CssCompletionData()
        {
            var help_file = CSScriptHelper.GetCSSHelp();

            if (File.Exists(help_file))
            {
                var text = File.ReadAllText(help_file);

                int prevIndex = -1;

                foreach (Match match in Regex.Matches(text, "(\r+|\n+)//css_"))
                {
                    if (prevIndex != -1)
                    {
                        // sections are terminated by "-------------------" lines
                        var section       = text.Substring(prevIndex, match.Index - prevIndex).TrimEnd().TrimEnd('-');
                        var section_lines = Regex.Split(section, "\r\n|\r|\n");

                        var id = section_lines[0].Split(' ').First(); // '//css_include <file>;'

                        var alt_id = section_lines.Where(x => x.StartsWith("Alias - //css"))
                                     .Select(x => x.Replace("Alias - ", "").Trim())
                                     .FirstOrDefault();

                        help_map[id] = new SectionInfo {
                            docOffset = prevIndex, text = section
                        };
                        if (alt_id != null)
                        {
                            help_map[alt_id] = help_map[id];
                        }
                    }

                    prevIndex = match.Index + 1; // +1 because of the '/n' or '/n' prefix
                }

                AllDirectives = help_map.Select(x =>
                                                new CssCompletionData
                {
                    CompletionText = x.Key,
                    DisplayText    = x.Key,
                    Description    = x.Value.text
                })
                                .ToArray();
            }
        }
예제 #8
0
        static string GenerateProjectFor(string script)
        {
            //MessageBox.Show(typeof(Project).Assembly.Location, typeof(Project).Assembly.GetName().ToString());

            var result = new StringBuilder();

            Project project = CSScriptHelper.GenerateProjectFor(new SourceInfo(script));

            foreach (string file in project.Files)
            {
                result.AppendLine($"file:{file}");
            }

            foreach (string file in project.Refs)
            {
                result.AppendLine($"ref:{file}");
            }

            return(result.ToString().Trim());
        }
예제 #9
0
        internal static string GetMemberInfo(string scriptFile, int caret, bool collapseOverloads)
        {
            // Complete API for N++

            Output.WriteLine("GetMemberInfo");
            //Console.WriteLine("hint: " + hint);

            var    script = new SourceInfo(scriptFile);
            string result = null;

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            void loockupDirective(string directive)
            {
                var css_directive = CssCompletionData.AllDirectives
                                    .FirstOrDefault(x => x.DisplayText == directive);

                if (css_directive != null)
                {
                    result = $"Directive: {css_directive.DisplayText}\n{css_directive.Description}";
                    result = result.NormalizeLineEnding().Replace("\r\n\r\n", "\r\n").TrimEnd();
                }
            };

            ParseAsCssDirective(script.Content, caret,
                                loockupDirective,
                                (directive, arg, extensions) =>
            {
                if (LookopDirectivePath(script, caret, directive, arg) is string file)
                {
                    result = $"File: {file}";
                }
                else
                {
                    loockupDirective(directive);
                }
            });

            if (result.HasText())
            {
                return(MemberInfoData.Serialize(new MemberInfoData {
                    Info = result
                }));
            }

            if (!script.RawFile.EndsWith(".g.cs"))
            {
                CSScriptHelper.DecorateIfRequired(ref script.Content, ref caret);
            }

            Project project = CSScriptHelper.GenerateProjectFor(script);
            var     sources = project.Files
                              .Where(f => f != script.File)
                              .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                              .ToArray();

            int methodStartPosTemp;
            var items = Autocompleter.GetMemberInfo(script.Content, caret, out methodStartPosTemp, project.Refs, sources, includeOverloads: !collapseOverloads);

            if (collapseOverloads)
            {
                items = items.Take(1);
            }

            result = items.Select(x => new MemberInfoData {
                Info = x, MemberStartPosition = methodStartPosTemp
            })
                     .Select(MemberInfoData.Serialize)
                     .JoinSerializedLines();

            return(result);
        }
예제 #10
0
        internal static string GetTooltip(string scriptFile, int caret, string hint, bool shortHintedTooltips)
        {
            // Simplified API for ST3
            Output.WriteLine("GetTooltip");
            //Console.WriteLine("hint: " + hint);

            string result = null;

            var script = new SourceInfo(scriptFile);

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            void loockupDirective(string directive)
            {
                var css_directive = CssCompletionData.AllDirectives
                                    .FirstOrDefault(x => x.DisplayText == directive);

                if (css_directive != null)
                {
                    result = $"Directive: {css_directive.DisplayText}\n{css_directive.Description}";
                    result = result.NormalizeLineEnding().Replace("\r\n\r\n", "\r\n").TrimEnd();
                }
            };

            ParseAsCssDirective(script.Content, caret,
                                loockupDirective,
                                (directive, arg, extensions) =>
            {
                if (LookopDirectivePath(script, caret, directive, arg) is string file)
                {
                    result = $"File: {file}";
                }
                else
                {
                    loockupDirective(directive);
                }
            });

            if (result.HasText())
            {
                return(result);
            }

            if (!script.RawFile.EndsWith(".g.cs"))
            {
                CSScriptHelper.DecorateIfRequired(ref script.Content, ref caret);
            }

            Project project = CSScriptHelper.GenerateProjectFor(script);
            var     sources = project.Files
                              .Where(f => f != project.Script)
                              .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                              .ToArray();

            int methodStartPosTemp;
            var items = Autocompleter.GetMemberInfo(script.Content, caret, out methodStartPosTemp, project.Refs, sources, includeOverloads: hint.HasAny());

            fullyLoaded = true;
            if (hint.HasAny())
            {
                if (shortHintedTooltips)
                {
                    items = items.Select(x => x.Split('\n').FirstOrDefault()).ToArray();
                }

                int count = hint.Split(',').Count();
                result = items.FirstOrDefault(x =>
                {
                    return(SyntaxMapper.GetArgumentCount(x) == count);
                })
                         ?? items.FirstOrDefault();

                bool hideOverloadsSummary = false;
                if (result != null && hideOverloadsSummary)
                {
                    var lines = result.Split('\n').Select(x => x.TrimEnd('\r')).ToArray();
                    //(+ 1 overloads)
                    if (lines[0].EndsWith(" overloads)"))
                    {
                        try
                        {
                            lines[0] = lines[0].Split(new[] { "(+" }, StringSplitOptions.None).First().Trim();
                        }
                        catch { }
                    }
                    result = lines.JoinBy("\n");
                }
            }
            else
            {
                result = items.FirstOrDefault();
            }

            if (result.HasText())
            {
                result = result.NormalizeLineEnding().Replace("\r\n\r\n", "\r\n").TrimEnd();
            }

            return(result);
        }
예제 #11
0
        internal static IEnumerable <ICompletionData> GetCompletionRaw(string scriptFile, int caret, bool includDocumentation = false)
        {
            var script = new SourceInfo(scriptFile);

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            IEnumerable <ICompletionData> completions = null;

            bool wasHandled = ParseAsCssDirective(script.Content, caret,
                                                  (directive) =>
            {
                completions = CssCompletionData.AllDirectives;
            },
                                                  (directive, arg, extensions) =>
            {
                if (directive.OneOf("//css_ac", "//css_autoclass"))
                {
                    completions = new[]
                    {
                        new CssCompletionData
                        {
                            CompletionText = "freestyle",
                            DisplayText    = "freestyle",
                            Description    = "Free style code without any entry point.",
                            CompletionType = CompletionType.directive
                        }
                    };
                }
                else
                {
                    completions = LookupDirectivePaths(script, caret, directive, arg, extensions)
                                  .Select(file => new CssCompletionData
                    {
                        CompletionText = Path.GetFileName(file),
                        DisplayText    = Path.GetFileName(file),
                        Description    = "File: " + file,
                        CompletionType = CompletionType.file
                    });
                }
            },
                                                  ignoreEmptyArgs: false);

            if (!wasHandled)
            {
                if (!script.RawFile.EndsWith(".g.cs"))
                {
                    CSScriptHelper.DecorateIfRequired(ref script.Content, ref caret);
                }

                Project project = CSScriptHelper.GenerateProjectFor(script);
                var     sources = project.Files
                                  .Where(f => f != project.Script)
                                  .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                                  .ToArray();

                completions = Autocompleter.GetAutocompletionFor(script.Content, caret, project.Refs, sources, includDocumentation);

                var count = completions.Count();
            }
            return(completions);
        }
예제 #12
0
        internal static string CodeMap(string script, bool nppSerialization, bool vsCodeSerialization)
        {
            csscript.Log("CodeMap");

            bool vs_code = vsCodeSerialization;
            bool sublime = !vsCodeSerialization && !nppSerialization;

            string code = File.ReadAllText(script);

            if (code.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            CodeMapItem[] members = CSScriptHelper.GetMapOf(code, nppSerialization).OrderBy(x => x.ParentDisplayName).ToArray();

            fullyLoaded = true;

            if (nppSerialization)
            {
                return(members.Select(CodeMapItem.Serialize).JoinSerializedLines());
            }

            if (vs_code)
            {
                // https://github.com/oleg-shilo/codemap.vscode/wiki/Adding-custom-mappers
                // [indent]<name>|<line>|<icon>

                //see "static AutoCompiler.CodeMapItem[] GetMapOfCSharp(string code, bool decorated)" for possible MemberType

                var result = new StringBuilder();
                var lines  = members.Select(x =>
                                            new
                {
                    ParentType     = (x.ParentDisplayName).Trim(),
                    ParentTypeIcon = x.ParentDisplayType == "interface" ? "interface" : "class",
                    Indent         = "  ",
                    Name           = x.DisplayName,
                    Line           = x.Line,
                    Icon           = (x.MemberType == "Enum" ? "class" :
                                      x.MemberType == "Method" ? "function" :
                                      x.MemberType == "Property" ? "property" :
                                      x.MemberType == "Field" ? "field" :
                                      "none")
                });

                if (lines.Any())
                {
                    foreach (var group in lines.GroupBy(x => x.ParentType))
                    {
                        bool first = true;
                        foreach (var item in group)
                        {
                            var parentType = group.Key;
                            if (first)
                            {
                                // do not pass any line number for class as it may be declared
                                // at multiple locations (e.g. partial classes)
                                first = false;
                                result.AppendLine($"{parentType}||{item.ParentTypeIcon}");
                            }
                            result.AppendLine($"{item.Indent}{item.Name}|{item.Line}|{item.Icon}");
                        }
                    }
                }
                return(result.ToString().Trim());
            }

            if (sublime)
            {
                var result = new StringBuilder();
                var lines  = members.Select(x =>
                {
                    return(new
                    {
                        Type = (x.ParentDisplayType + " " + x.ParentDisplayName).Trim(),
                        Content = "    " + x.DisplayName,
                        Line = x.Line
                    });
                });

                if (lines.Any())
                {
                    int maxLenghth = lines.Select(x => x.Type.Length).Max();
                    maxLenghth = Math.Max(maxLenghth, lines.Select(x => x.Content.Length).Max());

                    string prevType = null;
                    foreach (var item in lines)
                    {
                        if (prevType != item.Type)
                        {
                            result.AppendLine();
                            result.AppendLine(item.Type);
                        }

                        prevType = item.Type;
                        var suffix = new string(' ', maxLenghth - item.Content.Length);
                        result.AppendLine($"{item.Content}{suffix} :{item.Line}");
                    }
                }
                return(result.ToString().Trim());
            }

            return(null);
        }
예제 #13
0
 // "project" - request
 public static Project GenerateProjectFor(string script)
 => CSScriptHelper.GenerateProjectFor(new SourceInfo(script));