Esempio n. 1
0
        private (Document document, int offset) GetDocumentWithOffsetFromCode(string code)
        {
            var compilation  = ScriptState.Script.GetCompilation();
            var originalCode = ScriptState.Script.Code ?? string.Empty;

            var buffer = new StringBuilder(originalCode);

            if (!string.IsNullOrWhiteSpace(originalCode) && !originalCode.EndsWith(Environment.NewLine))
            {
                buffer.AppendLine();
            }

            var offset = buffer.Length;

            buffer.AppendLine(code);
            var fullScriptCode = buffer.ToString();

            if (_fixture == null || ShouldRebuild())
            {
                _fixture = new WorkspaceFixture(compilation.Options, compilation.References);
            }

            var document = _fixture.ForkDocument(fullScriptCode);

            return(document, offset);

            bool ShouldRebuild()
            {
                return(compilation.References.Count() != _fixture.MetadataReferences.Count());
            }
        }
Esempio n. 2
0
        private async Task <IEnumerable <CompletionItem> > GetCompletionList(
            string code,
            int cursorPosition)
        {
            if (ScriptState == null)
            {
                ScriptState = await CSharpScript.RunAsync(string.Empty, ScriptOptions);
            }

            var compilation  = ScriptState.Script.GetCompilation();
            var originalCode =
                ScriptState?.Script.Code ?? string.Empty;

            var buffer = new StringBuilder(originalCode);

            if (!string.IsNullOrWhiteSpace(originalCode) && !originalCode.EndsWith(Environment.NewLine))
            {
                buffer.AppendLine();
            }

            buffer.AppendLine(code);
            var fullScriptCode   = buffer.ToString();
            var offset           = fullScriptCode.LastIndexOf(code, StringComparison.InvariantCulture);
            var absolutePosition = Math.Max(offset, 0) + cursorPosition;

            if (_fixture == null || ShouldRebuild())
            {
                _fixture = new WorkspaceFixture(compilation.Options, compilation.References);
            }

            var document = _fixture.ForkDocument(fullScriptCode);
            var service  = CompletionService.GetService(document);

            var completionList = await service.GetCompletionsAsync(document, absolutePosition);

            var semanticModel = await document.GetSemanticModelAsync();

            var symbols = await Recommender.GetRecommendedSymbolsAtPositionAsync(semanticModel, absolutePosition, document.Project.Solution.Workspace);

            var symbolToSymbolKey = new Dictionary <(string, int), ISymbol>();

            foreach (var symbol in symbols)
            {
                var key = (symbol.Name, (int)symbol.Kind);
                if (!symbolToSymbolKey.ContainsKey(key))
                {
                    symbolToSymbolKey[key] = symbol;
                }
            }
            var items = completionList.Items.Select(item => item.ToModel(symbolToSymbolKey, document)).ToArray();

            return(items);

            bool ShouldRebuild()
            {
                return(compilation.References.Count() != _fixture.MetadataReferences.Count());
            }
        }