protected override bool AddLookupItems()

in resharper/src/CSharp/Completions/GodotResourcePathCodeCompletion.cs [41:113]


        protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector)
        {
            if (!IsAvailable(context))
                return false;

            var project = context.NodeInFile.GetProject();
            if (project is null)
                return false;

            if (!project.IsGodotProject())
                return false;

            //return Logger.CatchSilent(() =>
            {
                var projectPath = project.ProjectLocationLive.Value;
                if (projectPath is null) 
                    return false;
                var stringLiteral = context.StringLiteral();
                if (stringLiteral is null)
                    return false;

                var originalString = string.Empty;
                if (stringLiteral.ConstantValue.AsString() is string os)
                {
                    originalString = os;
                }

                var relativePathString = string.Empty;
                if (originalString.StartsWith(Prefix)) 
                    relativePathString = originalString.Substring(Prefix.Length);
                var searchPath = VirtualFileSystemPath.ParseRelativelyTo(relativePathString, projectPath);

                var completions = FullPathCompletions(context, searchPath).ToList();

                // If path leads outside project (e.g., due to `..` going up too many levels), don't provide completions.
                if (!projectPath.IsPrefixOf(searchPath))
                {
                    return false;
                }

                if (originalString.StartsWith(Prefix))
                {
                    completions.AddRange(OneLevelPathCompletions(searchPath));
                }
                
                var items = 
                    (from completion in completions.Distinct() 
                     select new ResourcePathItem(projectPath, completion, context.CompletionRanges))
                    .ToList();
                foreach (var item in items)
                {
                    collector.Add(item);
                }
                
                if (!originalString.StartsWith(Prefix) && completions.Any())
                {
                    // workarounds RIDER-90857
                    var resItem = new StringLiteralItem(Prefix);
                    var ranges = context.CompletionRanges;
                    var documentRange = new DocumentRange(ranges.InsertRange.StartOffset + 1,
                        ranges.InsertRange.EndOffset + 1);
                    var replaceRange = new DocumentRange(ranges.ReplaceRange.StartOffset + 1,
                        ranges.ReplaceRange.EndOffset - 1);
                    // ReSharper disable once RedundantArgumentDefaultValue
                    var range = new TextLookupRanges(documentRange, replaceRange, true);

                    resItem.InitializeRanges(range, context.BasicContext);
                    collector.Add(resItem);
                }
                return !items.IsEmpty();
            }
                //);
        }