public ParseInformation Parse( FileName fileName, ITextSource fileContent, TypeScriptProject project, IEnumerable <TypeScriptFile> files) { try { using (TypeScriptContext context = contextFactory.CreateContext()) { context.AddFile(fileName, fileContent.Text); context.RunInitialisationScript(); NavigationBarItem[] navigation = context.GetNavigationInfo(fileName); var unresolvedFile = new TypeScriptUnresolvedFile(fileName); unresolvedFile.AddNavigation(navigation, fileContent); if (project != null) { context.AddFiles(files); var document = new TextDocument(fileContent); Diagnostic[] diagnostics = context.GetDiagnostics(fileName, project.GetOptions()); TypeScriptService.TaskService.Update(diagnostics, fileName); } return(new ParseInformation(unresolvedFile, fileContent.Version, true)); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); LoggingService.Debug(ex.ToString()); } return(new ParseInformation( new TypeScriptUnresolvedFile(fileName), fileContent.Version, true)); }
private void ProcessProperty(TypeScriptContext tsc, ClassDeclaration md, HtmlNode h, string sn) { var div = h.ParentNode; var vd = new PropertyDeclaration() { // IsConstant = div.SelectSingleNode("div/span[@class='label label-constant']") != null, Name = sn }; var pt = ResolveType(tsc, div.SelectSingleNode("*[@class='type-signature']")?.InnerText.Trim()); vd.IsRequired = !pt.IsNullable; vd.PropertyType = pt.Type; var dsc = div.SelectNodes("p[not(@class)]").SanitizeDocumentation(); if (dsc != null) { vd.Documentation = new Documentation() { Summary = dsc }; } md.Members.Add(vd); MemberParsed?.Invoke(this, new MemberEventArgs(vd)); }
private static async Task RunMain() { var tsCtx = new TypeScriptContext(); await tsCtx.LoadComponentsAsync(); const string mainFileName = "main.ts"; //Load script string script = @" class Program { static main() { console.log(""Hello, World!""); } static other() { console.log(""Hello, Other World!""); } } Program.main(); Progra Program. Program.ot "; tsCtx.OpenFile(mainFileName, script); var classNameCompletionInfo = await tsCtx.GetCompletionsAtPositionAsync(mainFileName, 192 /* The 'Progra' line*/); var methodCompletionInfo = await tsCtx.GetCompletionsAtPositionAsync(mainFileName, 202 /* The 'Program.' line*/); var noCompletionsInfo = await tsCtx.GetCompletionsAtPositionAsync(mainFileName, script.Length - 2); }
private void ProcessModuleProperty(TypeScriptContext tsc, ModuleDeclaration md, HtmlNode h, string sn) { var div = h.ParentNode; var typeText = div.SelectSingleNode("*[@class='type-signature']")?.InnerText.Trim(); var vd = new VariableDeclaration() { IsConstant = div.SelectSingleNode("div/span[@class='label label-constant']") != null, Name = sn, VariableType = string.IsNullOrEmpty(typeText) ? BuiltinType.Any : ResolveType(tsc, typeText).Type }; var dsc = div.SelectNodes("p[not(@class)]").SanitizeDocumentation(); if (dsc != null) { vd.Documentation = new Documentation() { Summary = dsc }; } md.Statements.Add(vd); StatementParsed?.Invoke(this, new StatementEventArgs(vd)); }
void RemoveTypeScriptFileFromContext(FileName fileName) { if (TypeScriptParser.IsTypeScriptFileName(fileName)) { TypeScriptContext context = TypeScriptService.ContextProvider.GetContext(fileName); context.RemoveFile(fileName); } }
void UpdateTypeScriptContextWithFileContentFromDisk(FileName fileName) { if (File.Exists(fileName)) { TypeScriptContext context = TypeScriptService.ContextProvider.GetContext(fileName); string fileContent = FileReader.ReadFileContent(fileName, SD.FileService.DefaultFileEncoding); context.UpdateFile(fileName, fileContent); } }
private void ProcessMethod(TypeScriptContext tsc, ClassDeclaration md, HtmlNode h, string sn) { var div = h.ParentNode; var fd = new MethodDeclaration(); ProcessFunctionCore(tsc, sn, div, fd); md.Members.Add(fd); MemberParsed?.Invoke(this, new MemberEventArgs(fd)); }
private void ProcessModuleMethod(TypeScriptContext tsc, ModuleDeclaration md, HtmlNode h, string sn) { var div = h.ParentNode; var fd = new FunctionDeclaration(); ProcessFunctionCore(tsc, sn, div, fd); md.Statements.Add(fd); StatementParsed?.Invoke(this, new StatementEventArgs(fd)); }
bool ShowCompletion(ITextEditor editor, bool memberCompletion) { TypeScriptContext context = GetContext(editor); UpdateContext(context, editor); var completionProvider = new TypeScriptCompletionItemProvider(context); return(completionProvider.ShowCompletion(editor, memberCompletion)); }
static void UpdateContext(TypeScriptContext context, ITextEditor editor) { if (IsFileInsideProject(editor.FileName)) { UpdateAllOpenFiles(context); } else { context.UpdateFile(editor.FileName, editor.Document.Text); } }
public static List <Reference> GetReferences(ITextEditor editor) { TypeScriptContext context = GetContext(editor); UpdateContext(context, editor); ReferenceEntry[] entries = context.FindReferences(editor.FileName, editor.Caret.Offset); return(entries .Select(entry => CreateReference(entry)) .ToList()); }
public static void GoToDefinition(ITextEditor editor) { TypeScriptContext context = GetContext(editor); UpdateContext(context, editor); DefinitionInfo[] definitions = context.GetDefinition(editor.FileName, editor.Caret.Offset); if ((definitions != null) && (definitions.Length > 0)) { GoToDefinition(definitions[0]); } }
static void UpdateAllOpenFiles(TypeScriptContext context) { foreach (IViewContent view in SD.Workbench.ViewContentCollection) { if (TypeScriptParser.IsTypeScriptFileName(view.PrimaryFileName)) { if (IsFileInsideProject(view.PrimaryFileName)) { UpdateContext(context, view.PrimaryFileName); } } } }
private FlagedType ResolveType(TypeScriptContext context, string name) { var e = new TypeResolveEventArgs(name); e.Type = context.ResolveType(name, n => ResolveType(context, n)); TypeResolved?.Invoke(this, e); if (e.Type.Type == null) { throw new ArgumentException(string.Format("'{0}'が解決できませんでした。", name)); } return(e.Type); }
public TypeScriptProject() : base(true) { ExcludedFiles = new List <string>(); Items = new ObservableCollection <IProjectItem>(); References = new ObservableCollection <IProject>(); ToolchainSettings = new ExpandoObject(); DebugSettings = new ExpandoObject(); Project = this; var tsContext = new TypeScriptContext(); tsContext.LoadComponents(); IoC.RegisterConstant(tsContext, typeof(TypeScriptContext)); TypeScriptContext = tsContext; }
void ShowMethodInsight(ITextEditor editor) { TypeScriptContext context = GetContext(editor); UpdateContext(context, editor); var provider = new TypeScriptFunctionInsightProvider(context); IInsightItem[] items = provider.ProvideInsight(editor); IInsightWindow insightWindow = editor.ShowInsightWindow(items); // if (insightWindow != null) { // insightHandler.InitializeOpenedInsightWindow(editor, insightWindow); // insightHandler.HighlightParameter(insightWindow, 0); // } }
public void RegisterSourceFile(AvaloniaEdit.TextEditor editor, ISourceFile file, TextDocument textDocument) { _typeScriptContext = _typeScriptContext ?? ((TypeScriptProject)file.Project).TypeScriptContext; _typeScriptContext.OpenFile(file.FilePath, File.ReadAllText(file.FilePath)); TypeScriptDataAssociation association = null; if (dataAssociations.TryGetValue(file, out association)) { throw new InvalidOperationException("Source file already registered with language service."); } association = new TypeScriptDataAssociation(); dataAssociations.Add(file, association); }
public void Compile(FileName fileName, TypeScriptProject project, TypeScriptContext context) { ReportCompileStarting(fileName); project.CreateOutputDirectory(); var compiler = new LanguageServiceCompiler(context); UpdateFile(context, fileName); LanguageServiceCompilerResult result = compiler.Compile(fileName, project); if (result.HasErrors) { Report(result.GetError()); } ReportCompileFinished(result.HasErrors); }
public static List <SearchResultMatch> GetReferences(ITextEditor editor) { TypeScriptContext context = GetContext(editor); UpdateContext(context, editor); ReferenceEntry[] entries = context.FindReferences(editor.FileName, editor.Caret.Offset); if (entries == null) { return(new List <SearchResultMatch>()); } return(entries .Select(entry => CreateSearchResultMatch(entry)) .ToList()); }
private static async Task RunMain() { var tsCtx = new TypeScriptContext(); await tsCtx.LoadComponentsAsync(); var scriptSrc = @" class Program { static main() { console.log(""Hello, World!""); } } class Person { constructor(public name: string) { } public sayHello() { console.log(this.name + "" says hello!""); } } class IceCreamSandwich { constructor(public size: number) { } public add(a: number, b: number) { return a + b; } } let bob = new Person(""Bob""); bob.sayHello(); Program.main(); let num = (new IceCreamSandwich(11)).add(1, 2); // num = ""error""; "; var astJson = tsCtx.BuildAstJson("main.ts", scriptSrc); Console.WriteLine(astJson); Console.WriteLine("Analyzing and deserializing AST JSON:"); var tsAst = tsCtx.BuildAst("main.ts", scriptSrc); }
public async Task <TypeScriptContext> LoadAsync(string rootUrl) { using (var hc = new HttpClient()) { var urls = await GetUrlListAsync(hc, rootUrl); var tsc = new TypeScriptContext(); foreach (var url in urls) { var html = await DownloadContentAsync(hc, url); if (html == null) { continue; } var hd = new HtmlDocument(); hd.LoadHtml(html); var m = hd.DocumentNode.SelectSingleNode("//*[@id='jsdoc-main']"); var fullName = m.SelectSingleNode("header/h1").InnerText.Trim(); var kind = m.SelectSingleNode("header//*[@class='label label-kind']").InnerText?.Trim(); var desc = m.SelectNodes("header//p").SanitizeDocumentation(); var section = m.SelectSingleNode("section"); switch (kind.ToLowerInvariant()) { case "namespace": ParseNamespaceHtml(tsc, fullName, desc, section); break; case "class": ParseClassHtml(tsc, fullName, desc, section); break; } } return(tsc); } }
private void ProcessFunctionCore(TypeScriptContext tsc, string name, HtmlNode div, ITypeScriptFunction fd) { fd.Name = name; fd.Documentation = new Documentation(); fd.Documentation.Summary = div.SelectNodes("p[not(@class)]").SanitizeDocumentation(); var returnValue = div.SelectSingleNode("dl/dd/p"); if (returnValue != null) { fd.ReturnType = ResolveType(tsc, returnValue.SelectSingleNode("code").InnerText).Type; fd.Documentation.Returns = returnValue.SelectNodes("text()").SanitizeDocumentation(); } var trs = div.SelectNodes("section//table/tbody/tr"); if (trs != null) { foreach (var tr in trs) { var mn = tr.SelectSingleNode("td[1]")?.InnerText?.Trim(); if (mn != null) { var pt = ResolveType(tsc, tr.SelectSingleNode("td[2]/p[@class='details-table-types']")?.InnerText?.Trim()); fd.Parameters.Add(new ParameterDeclaration() { Name = mn, ParameterType = pt.Type, IsRequired = tr.SelectSingleNode("td[2]/p[@class='details-table-optional']") == null && !pt.IsNullable }); fd.Documentation.Parameters.Add(new ParameterDocumentation() { ParameterName = mn, Description = tr.SelectNodes("td[2]/p[not(@class)]").SanitizeDocumentation() }); } } } }
void FileSaved(object sender, FileNameEventArgs e) { if (!TypeScriptFileExtensions.IsTypeScriptFileName(e.FileName)) { return; } TypeScriptProject project = TypeScriptService.GetProjectForFile(e.FileName); if (project == null) { return; } if (project.CompileOnSave) { var action = new CompileTypeScriptOnSaveFileAction(); TypeScriptContext context = TypeScriptService.ContextProvider.GetContext(e.FileName); action.Compile(e.FileName, project, context); } }
void CompileFiles(TypeScriptProject project, FileName[] fileNames) { ReportCompileStarting(project); bool errors = false; TypeScriptContext context = TypeScriptService.ContextProvider.GetContext(fileNames.First()); var compiler = new LanguageServiceCompiler(context); project.CreateOutputDirectory(); foreach (FileName fileName in fileNames) { UpdateFile(context, fileName); LanguageServiceCompilerResult result = compiler.Compile(fileName, project); if (result.HasErrors) { errors = true; Report(result.GetError()); } } ReportCompileFinished(errors); }
public ICompilationUnit Parse( IProjectContent projectContent, string fileName, ITextBuffer fileContent, IEnumerable <TypeScriptFile> files) { try { using (TypeScriptContext context = contextFactory.CreateContext()) { var file = new FileName(fileName); context.AddFile(file, fileContent.Text); context.RunInitialisationScript(); NavigationBarItem[] navigation = context.GetNavigationInfo(file); var unit = new TypeScriptCompilationUnit(projectContent) { FileName = fileName }; unit.AddNavigation(navigation, fileContent); var typeScriptProjectContent = projectContent as TypeScriptProjectContent; if (typeScriptProjectContent != null) { context.AddFiles(files); IDocument document = DocumentUtilitites.LoadReadOnlyDocumentFromBuffer(fileContent); Diagnostic[] diagnostics = context.GetDiagnostics(file, typeScriptProjectContent.Options); TypeScriptService.TaskService.Update(diagnostics, file, document); } return(unit); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); LoggingService.Debug(ex.ToString()); } return(new DefaultCompilationUnit(projectContent)); }
public TypeScriptFunctionInsightProvider(TypeScriptContext context) { this.context = context; }
private void ProcessEnum(TypeScriptContext tsc, ModuleDeclaration module, HtmlNode h3, string name) { var div = h3.ParentNode; var enumType = div.SelectSingleNode("*[@class='type-signature']")?.InnerText.Trim(); var desc = div.SelectNodes("p[not(@class)]").SanitizeDocumentation(); TypeDeclaration td; ITypeScriptType ft; IList fs; if (!StringEnumAsClass || enumType == null || enumType.Equals("number")) { var ed = new EnumDeclaration(); td = ed; ft = ed; fs = ed.Members; } else { var ct = new ClassDeclaration(); td = ct; ft = ResolveType(tsc, enumType).Type; fs = ct.Members; } td.IsExport = true; td.Name = name; if (desc != null) { td.Documentation = new Documentation() { Summary = desc }; } module.Statements.Add(td); var e = new TypeEventArgs(td); TypeParsing?.Invoke(this, e); var trs = div.SelectNodes("section//table/tbody/tr"); if (trs != null) { foreach (var tr in trs) { var mn = tr.SelectSingleNode("td[1]")?.InnerText?.Trim(); if (mn != null) { var md = tr.SelectNodes("td[2]").SanitizeDocumentation(); var f = new FieldDeclaration(); f.AccessModifier = AccessModifier.Public; f.IsStatic = true; f.Name = mn; f.FieldType = ft; if (md != null) { f.Documentation = new Documentation() { Summary = md }; } fs.Add(f); } } } TypeParsed?.Invoke(this, e); }
private void ParseClassHtml(TypeScriptContext tsc, string fullName, string desc, HtmlNode section) { var cl = tsc.FindType(fullName) as ClassDeclaration; if (cl == null) { var i = fullName.LastIndexOf('.'); var md = tsc.GetModule(fullName.Substring(0, i)); cl = new ClassDeclaration() { Name = fullName.Substring(i + 1), IsExport = true }; md.Statements.Add(cl); } if (cl.Documentation == null && desc != null) { cl.Documentation = new Documentation() { Summary = desc }; } var e = new TypeEventArgs(cl); TypeParsing?.Invoke(this, e); var headers = section.SelectNodes("//h2|//h3[@class='symbol-name']"); if (headers != null) { var t = H2.None; foreach (var h in headers) { if (h.Name.Equals("h2", StringComparison.InvariantCultureIgnoreCase)) { switch (h.InnerText.Trim().ToLowerInvariant()) { case "constructor": t = H2.Constructor; break; case "enumerations": case "enumeration": t = H2.Enum; break; case "properties": case "property": t = H2.Property; break; case "methods": case "method": t = H2.Method; break; case "namespaces": case "namespace": t = H2.Namespace; break; default: t = H2.None; Debugger.Break(); break; } } else { var sn = h.InnerText.Trim(); switch (t) { case H2.Constructor: ProcessConstructor(tsc, cl, h, sn); break; case H2.Property: ProcessProperty(tsc, cl, h, sn); break; case H2.Method: ProcessMethod(tsc, cl, h, sn); break; default: break; } } } } TypeParsed?.Invoke(this, e); }
public TypeScriptCompletionItemProvider(TypeScriptContext context) { this.context = context; }
private void ParseNamespaceHtml(TypeScriptContext tsc, string fullName, string desc, HtmlNode section) { var md = new ModuleDeclaration() { Name = fullName }; if (desc != null) { md.Documentation = new Documentation() { Summary = desc }; } tsc.Statements.Add(md); var e = new ModuleEventArgs(md); ModuleParsing?.Invoke(this, e); var headers = section.SelectNodes("//h2|//h3[@class='symbol-name']"); if (headers != null) { var t = H2.None; foreach (var h in headers) { if (h.Name.Equals("h2", StringComparison.InvariantCultureIgnoreCase)) { switch (h.InnerText.Trim().ToLowerInvariant()) { case "classes": case "class": t = H2.Class; break; case "enumerations": case "enumeration": t = H2.Enum; break; case "properties": case "property": t = H2.Property; break; case "methods": case "method": t = H2.Method; break; case "namespaces": case "namespace": t = H2.Namespace; break; case "abstract types": case "abstract type": t = H2.AbstractType; break; default: t = H2.None; break; } } else { var sn = h.InnerText.Trim(); switch (t) { case H2.Class: case H2.AbstractType: if (tsc.FindType(sn) == null) { var cl = new ClassDeclaration() { Name = sn.Split('.').Last(), IsExport = true, IsAbstract = t == H2.AbstractType }; var dsc = h.ParentNode.SelectNodes("p[not(@class)]").SanitizeDocumentation(); if (dsc != null) { cl.Documentation = new Documentation() { Summary = dsc }; } md.Statements.Add(cl); } break; case H2.Enum: ProcessEnum(tsc, md, h, sn); break; case H2.Property: ProcessModuleProperty(tsc, md, h, sn); break; case H2.Method: ProcessModuleMethod(tsc, md, h, sn); break; default: break; } } } } ModuleParsed?.Invoke(this, e); }