Example #1
0
        public string ShowPosition(LineChr lineChr)
        {
            var pos      = GetPosition(lineChr);
            var lineText = ExtractLine(pos);

            return(ShowPositionOnLine(lineText, lineChr.Chr));
        }
        public List <string> GetCompletionKeywordsAtPosition(LineChr lineChr)
        {
            var analysisResult = GetAnalysis(lineChr);

            if (analysisResult.IsInsideComment || analysisResult.IsAfterAnyErrorLine(lineChr) || !analysisResult.SuccessfulRun)
            {
                return(new List <string>());
            }

            var typingToken = analysisResult.GetTokenBeingTypedAtCursor(lineChr);

            if (analysisResult.KeywordToken != null && analysisResult.KeywordToken != typingToken)
            {
                var fullAnalysis = GetAnalysis();
                return(fullAnalysis.NonKeywordWords);
            }

            var lastParent    = analysisResult.ConceptContext.LastOrDefault();
            var validConcepts = lastParent == null
                ? rhetosProjectContext.DslSyntax.ConceptTypes.ToList()
                : conceptQueries.ValidConceptsForParent(lastParent.Concept);

            var keywords = validConcepts
                           .Select(concept => concept.Keyword)
                           .Where(keyword => keyword != null)
                           .Distinct()
                           .OrderBy(keyword => keyword)
                           .ToList();

            return(keywords);
        }
Example #3
0
        // Due to unusual way the tokenizer works, if we capture errors during initial call to GetToken(),
        // valid tokens will be returned without error in subsequent calls
        private (ITokenizer tokenizer, List <CodeAnalysisError> capturedErrors) CreateTokenizerWithCapturedErrors()
        {
            var capturedErrors = new List <CodeAnalysisError>();

            try
            {
                var safeTokenizer   = new Tokenizer(textDocument, new FilesUtility(rhetosLogProvider), new Lazy <DslSyntax>(() => rhetosProjectContext.DslSyntax));
                var tokenizerResult = safeTokenizer.GetTokens();
                if (tokenizerResult.SyntaxError != null)
                {
                    var beginLineChr = new LineChr(tokenizerResult.SyntaxError.FilePosition.BeginLine - 1, tokenizerResult.SyntaxError.FilePosition.BeginColumn - 1);
                    var endLineChr   = new LineChr(tokenizerResult.SyntaxError.FilePosition.EndLine - 1, tokenizerResult.SyntaxError.FilePosition.EndColumn - 1);
                    capturedErrors.Add(new CodeAnalysisError()
                    {
                        BeginLineChr = beginLineChr, EndLineChr = endLineChr, Code = tokenizerResult.SyntaxError.ErrorCode, Message = tokenizerResult.SyntaxError.Message
                    });
                }
                return(new TokenizerExplicitTokens(tokenizerResult.Tokens), capturedErrors);
            }
            catch (Exception e)
            {
                capturedErrors.Add(new CodeAnalysisError()
                {
                    Message = e.Message
                });
            }

            return(new TokenizerExplicitTokens(new List <Token>()), capturedErrors);
        }
Example #4
0
 public Token GetTokenLeftOfPosition(LineChr lineChr)
 {
     if (lineChr.Chr > 0)
     {
         lineChr = new LineChr(lineChr.Line, lineChr.Chr - 1);
     }
     return(GetTokenAtPosition(lineChr));
 }
Example #5
0
        public Token GetTokenBeingTypedAtCursor(LineChr lineChr)
        {
            if (GetTokenAtPosition(lineChr) != null)
            {
                return(null);
            }

            return(GetTokenLeftOfPosition(lineChr));
        }
Example #6
0
        private CodeAnalysisError CreateAnalysisError(DslSyntaxException e)
        {
            var beginLineChr = new LineChr(e.FilePosition.BeginLine - 1, e.FilePosition.BeginColumn - 1);
            var endLineChr   = new LineChr(e.FilePosition.EndLine - 1, e.FilePosition.EndColumn - 1);

            return(new CodeAnalysisError()
            {
                BeginLineChr = beginLineChr, EndLineChr = endLineChr, Code = e.ErrorCode, Message = e.Message
            });
        }
Example #7
0
        public Token GetTokenAtPosition(LineChr lineChr)
        {
            var position = TextDocument.GetPosition(lineChr);

            foreach (var token in Tokens)
            {
                if (token.Type != TokenType.EndOfFile && position >= token.PositionInDslScript && position < token.PositionEndInDslScript)
                {
                    return(token);
                }
            }

            return(null);
        }
Example #8
0
        public string GetTruncatedAtNextEndOfLine(LineChr lineChr)
        {
            if (Text.Length == 0)
            {
                return("");
            }
            var pos = GetPosition(lineChr);

            while (pos < Text.Length)
            {
                if (Text[pos++] == '\n')
                {
                    break;
                }
            }

            return(Text.Substring(0, pos));
        }
Example #9
0
        private int GetActiveParameterForValidConcept(ConceptType conceptType)
        {
            var activeParameter = 0;

            // we have parsed some members successfully for this concept type
            if (LastTokenParsed.ContainsKey(conceptType))
            {
                activeParameter = ConceptTypeTools.IndexOfParameter(conceptType, LastMemberReadAttempt[conceptType]);

                // if we have just typed a keyword OR have stopped typing a parameter (by pressing space, etc.), we need to advance to next parameter
                // keyword scenario is possible in nested concepts, where we already have valid parameters and are just typing a keyword
                var lineChr      = new LineChr(Line, Chr);
                var atLastParsed = GetTokenAtPosition(lineChr) == LastTokenParsed[conceptType] || GetTokenLeftOfPosition(lineChr) == LastTokenParsed[conceptType];
                var atKeyword    = string.Equals(conceptType.Keyword, LastTokenParsed[conceptType].Value, StringComparison.InvariantCultureIgnoreCase);
                if (atKeyword || !atLastParsed)
                {
                    activeParameter++;
                }
            }

            return(activeParameter);
        }
Example #10
0
 public int GetPosition(LineChr lineChr)
 {
     return(GetPosition(lineChr.Line, lineChr.Chr));
 }
Example #11
0
 public bool IsAfterAnyErrorLine(LineChr lineChr)
 {
     return(AllErrors.Where(a => a.Severity == CodeAnalysisError.ErrorSeverity.Error).Any(error => lineChr.Line > error.BeginLineChr.Line));
 }
Example #12
0
        public bool IsAfterAnyErrorPosition(LineChr lineChr)
        {
            var pos = TextDocument.GetPosition(lineChr);

            return(AllErrors.Any(error => pos > TextDocument.GetPosition(error.BeginLineChr)));
        }
Example #13
0
        public (List <RhetosSignature> signatures, int?activeSignature, int?activeParameter) GetSignatureHelpAtPosition(LineChr lineChr)
        {
            var analysis = GetAnalysis(lineChr);

            if (analysis.KeywordToken == null || analysis.IsAfterAnyErrorLine(lineChr))
            {
                return(null, null, null);
            }

            var signaturesWithDocumentation = conceptQueries.GetSignaturesWithDocumentation(analysis.KeywordToken.Value);
            var validConcepts = analysis.GetValidConceptsWithActiveParameter();

            if (!validConcepts.Any())
            {
                return(signaturesWithDocumentation, null, null);
            }

            var sortedConcepts = validConcepts
                                 .Select(valid =>
                                         (
                                             valid.conceptType,
                                             valid.activeParamater,
                                             parameterCount: ConceptTypeTools.GetParameters(valid.conceptType).Count,
                                             documentation: signaturesWithDocumentation.Single(sig => sig.ConceptType == valid.conceptType)
                                         ))
                                 .OrderBy(valid => valid.activeParamater >= valid.parameterCount)
                                 .ThenBy(valid => valid.parameterCount)
                                 .ThenBy(valid => valid.conceptType.TypeName)
                                 .ToList();

            var activeParameter = sortedConcepts.First().activeParamater;

            return(sortedConcepts.Select(sorted => sorted.documentation).ToList(), 0, activeParameter);
        }
Example #14
0
        public (string description, LineChr startPosition, LineChr endPosition) GetHoverDescriptionAtPosition(LineChr lineChr)
        {
            var analysis = GetAnalysis(lineChr);

            if (analysis.KeywordToken == null || analysis.IsAfterAnyErrorLine(lineChr))
            {
                return(null, LineChr.Zero, LineChr.Zero);
            }

            var description = conceptQueries.GetFullDescription(analysis.KeywordToken.Value);

            if (string.IsNullOrEmpty(description))
            {
                description = $"No documentation found for '{analysis.KeywordToken.Value}'.";
            }

            var startPosition = TextDocument.GetLineChr(analysis.KeywordToken.PositionInDslScript);

            var endPosition = lineChr;

            if (analysis.NextKeywordToken != null)
            {
                endPosition = TextDocument.GetLineChr(analysis.NextKeywordToken.PositionInDslScript - 1);
            }

            return(description, startPosition, endPosition);
        }