public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested, IProject parentProject, CancellationToken cancellationToken) { var csharpProject = parentProject as AlProject; AlParser parser = new AlParser(csharpProject != null ? csharpProject.CompilerSettings : null); SyntaxTree cu = parser.Parse(fileContent, fileName); cu.Freeze(); AlUnresolvedFile file = cu.ToTypeSystem(); ParseInformation parseInfo; if (fullParseInformationRequested) parseInfo = new AlFullParseInformation(file, fileContent.Version, cu); else parseInfo = new ParseInformation(file, fileContent.Version, fullParseInformationRequested); IDocument document = fileContent as IDocument; AddCommentTags(cu, parseInfo.TagComments, fileContent, parseInfo.FileName, ref document); if (fullParseInformationRequested) { if (document == null) document = new ReadOnlyDocument(fileContent, parseInfo.FileName); ((AlFullParseInformation)parseInfo).newFoldings = CreateNewFoldings(cu, document); } return parseInfo; }
public ResolveResult Resolve(ParseInformation parseInfo, TextLocation location, ICompilation compilation, CancellationToken cancellationToken) { var csParseInfo = parseInfo as AlFullParseInformation; if (csParseInfo == null) throw new ArgumentException("Parse info does not have SyntaxTree"); AlUnresolvedFile unresolvedFile = csParseInfo.UnresolvedFile; var projectContents = compilation.Assemblies.Select(asm => asm.UnresolvedAssembly).OfType<IProjectContent>().ToList(); if (projectContents.All(pc => pc.GetFile(unresolvedFile.FileName) != unresolvedFile)) unresolvedFile = null; return ResolveAtLocation.Resolve(compilation, unresolvedFile, csParseInfo.SyntaxTree, location, cancellationToken); }
public AlFullParseInformation(AlUnresolvedFile unresolvedFile, ITextSourceVersion parsedVersion, SyntaxTree compilationUnit) : base(unresolvedFile, parsedVersion, isFullParseInformation: true) { if (unresolvedFile == null) { throw new ArgumentNullException("unresolvedFile"); } if (compilationUnit == null) { throw new ArgumentNullException("compilationUnit"); } this.syntaxTree = compilationUnit; }
public DefaultCompletionContextProvider(IDocument document, AlUnresolvedFile unresolvedFile) { if (document == null) { throw new ArgumentNullException("document"); } if (unresolvedFile == null) { throw new ArgumentNullException("unresolvedFile"); } this.document = document; this.unresolvedFile = unresolvedFile; }
public ICodeContext ResolveContext(ParseInformation parseInfo, TextLocation location, ICompilation compilation, CancellationToken cancellationToken) { var csParseInfo = parseInfo as AlFullParseInformation; if (csParseInfo == null) throw new ArgumentException("Parse info does not have SyntaxTree"); AlUnresolvedFile unresolvedFile = csParseInfo.UnresolvedFile; var projectContents = compilation.Assemblies.Select(asm => asm.UnresolvedAssembly).OfType<IProjectContent>().ToList(); if (projectContents.All(pc => pc.GetFile(unresolvedFile.FileName) != unresolvedFile)) unresolvedFile = null; var syntaxTree = csParseInfo.SyntaxTree; var node = syntaxTree.GetNodeAt(location); if (node == null) return null; // null result is allowed; the parser service will substitute a dummy context var resolver = new AlAstResolver(compilation, syntaxTree, unresolvedFile); return resolver.GetResolverStateBefore(node); }
public static AlCompletionContext Get(ITextEditor editor, ICodeContext context, TextLocation currentLocation, ITextSource fileContent) { IDocument document = new ReadOnlyDocument(fileContent); var projectContent = context.Compilation.MainAssembly.UnresolvedAssembly as IProjectContent; if (projectContent == null) { return(null); } AlParser parser = new AlParser(); parser.GenerateTypeSystemMode = false; SyntaxTree cu = parser.Parse(fileContent, Path.GetRandomFileName() + ".al"); cu.Freeze(); AlUnresolvedFile unresolvedFile = cu.ToTypeSystem(); ICompilation compilation = projectContent.AddOrUpdateFiles(unresolvedFile).CreateCompilation(SD.ParserService.GetCurrentSolutionSnapshot()); return(new AlCompletionContext(editor, EmptyList <string> .Instance, compilation, projectContent, document, unresolvedFile, currentLocation)); }
private AlCompletionContext(ITextEditor editor, IList <string> conditionalSymbols, ICompilation compilation, IProjectContent projectContent, IDocument document, AlUnresolvedFile unresolvedFile, TextLocation caretLocation) { Debug.Assert(editor != null); Debug.Assert(unresolvedFile != null); Debug.Assert(compilation != null); Debug.Assert(projectContent != null); Debug.Assert(document != null); this.Editor = editor; this.Document = document; this.ConditionalSymbols = conditionalSymbols; this.Compilation = compilation; this.ProjectContent = projectContent; this.TypeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, caretLocation); this.CompletionContextProvider = new DefaultCompletionContextProvider(document, unresolvedFile); this.CompletionContextProvider.ConditionalSymbols.AddRange(conditionalSymbols); }
void RenameReferencesInFile(SymbolRenameArgs args, IList <IFindReferenceSearchScope> searchScopeList, FileName fileName, Action <PatchedFile> callback, Action <Error> errorCallback, bool isNameValid, CancellationToken cancellationToken) { ITextSource textSource = args.ParseableFileContentFinder.Create(fileName); if (textSource == null) { return; } if (searchScopeList != null) { if (!searchScopeList.DistinctBy(scope => scope.SearchTerm ?? String.Empty).Any( scope => (scope.SearchTerm == null) || (textSource.IndexOf(scope.SearchTerm, 0, textSource.TextLength, StringComparison.Ordinal) >= 0))) { return; } } var parseInfo = SD.ParserService.Parse(fileName, textSource) as AlFullParseInformation; if (parseInfo == null) { return; } ReadOnlyDocument document = null; IHighlighter highlighter = null; List <RenameResultMatch> results = new List <RenameResultMatch>(); // Grab the unresolved file matching the compilation version // (this may differ from the version created by re-parsing the project) AlUnresolvedFile unresolvedFile = null; IProjectContent pc = compilation.MainAssembly.UnresolvedAssembly as IProjectContent; if (pc != null) { unresolvedFile = pc.GetFile(fileName) as AlUnresolvedFile; } AlAstResolver resolver = new AlAstResolver(compilation, parseInfo.SyntaxTree, unresolvedFile); fr.RenameReferencesInFile( searchScopeList, args.NewName, resolver, delegate(RenameCallbackArguments callbackArgs) { var node = callbackArgs.NodeToReplace; string newCode = callbackArgs.NewNode.ToString(); if (document == null) { document = new ReadOnlyDocument(textSource, fileName); if (args.ProvideHighlightedLine) { highlighter = SD.EditorControlService.CreateHighlighter(document); highlighter.BeginHighlighting(); } } var startLocation = node.StartLocation; var endLocation = node.EndLocation; int offset = document.GetOffset(startLocation); int length = document.GetOffset(endLocation) - offset; if (args.ProvideHighlightedLine) { var builder = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter); var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null; results.Add(new RenameResultMatch(fileName, startLocation, endLocation, offset, length, newCode, builder, defaultTextColor)); } else { results.Add(new RenameResultMatch(fileName, startLocation, endLocation, offset, length, newCode)); } }, errorCallback, cancellationToken); if (highlighter != null) { highlighter.Dispose(); } if (results.Count > 0) { if (!isNameValid) { errorCallback(new Error(ErrorType.Error, string.Format("The name '{0}' is not valid in the current context!", args.NewName), new DomRegion(fileName, results[0].StartLocation))); return; } IDocument changedDocument = new TextDocument(document); var oldVersion = changedDocument.Version; List <SearchResultMatch> fixedResults = new List <SearchResultMatch>(); int lastStartOffset = changedDocument.TextLength + 1; foreach (var result in results.OrderByDescending(m => m.StartOffset)) { if (result.EndOffset <= lastStartOffset) { changedDocument.Replace(result.StartOffset, result.Length, result.NewCode); fixedResults.Add(result); lastStartOffset = result.StartOffset; } } callback(new PatchedFile(fileName, fixedResults, oldVersion, changedDocument.Version)); } }
void FindReferencesInFile(SymbolSearchArgs args, IList <IFindReferenceSearchScope> searchScopeList, FileName fileName, Action <SearchedFile> callback, CancellationToken cancellationToken) { ITextSource textSource = args.ParseableFileContentFinder.Create(fileName); if (textSource == null) { return; } if (searchScopeList != null) { if (!searchScopeList.DistinctBy(scope => scope.SearchTerm ?? String.Empty).Any( scope => (scope.SearchTerm == null) || (textSource.IndexOf(scope.SearchTerm, 0, textSource.TextLength, StringComparison.Ordinal) >= 0))) { return; } } var parseInfo = SD.ParserService.Parse(fileName, textSource) as AlFullParseInformation; if (parseInfo == null) { return; } ReadOnlyDocument document = null; IHighlighter highlighter = null; List <SearchResultMatch> results = new List <SearchResultMatch>(); // Grab the unresolved file matching the compilation version // (this may differ from the version created by re-parsing the project) AlUnresolvedFile unresolvedFile = null; IProjectContent pc = compilation.MainAssembly.UnresolvedAssembly as IProjectContent; if (pc != null) { unresolvedFile = pc.GetFile(fileName) as AlUnresolvedFile; } fr.FindReferencesInFile( searchScopeList, unresolvedFile, parseInfo.SyntaxTree, compilation, delegate(AstNode node, ResolveResult result) { if (document == null) { document = new ReadOnlyDocument(textSource, fileName); highlighter = SD.EditorControlService.CreateHighlighter(document); highlighter.BeginHighlighting(); } Identifier identifier = node.GetChildByRole(Roles.Identifier); if (!identifier.IsNull) { node = identifier; } var region = new DomRegion(fileName, node.StartLocation, node.EndLocation); int offset = document.GetOffset(node.StartLocation); int length = document.GetOffset(node.EndLocation) - offset; var builder = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter); var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null; results.Add(new SearchResultMatch(fileName, node.StartLocation, node.EndLocation, offset, length, builder, defaultTextColor)); }, cancellationToken); if (highlighter != null) { highlighter.Dispose(); } if (results.Count > 0) { // Remove overlapping results List <SearchResultMatch> fixedResults = new List <SearchResultMatch>(); int lastEndOffset = 0; foreach (var result in results.OrderBy(m => m.StartOffset)) { if (result.StartOffset >= lastEndOffset) { fixedResults.Add(result); lastEndOffset = result.EndOffset; } } callback(new SearchedFile(fileName, fixedResults)); } }