Esempio n. 1
0
        // routines called by the file content manager upon updating a file

        /// <summary>
        /// Given the start line of a change, and how many lines have been updated from there,
        /// computes the position where the syntax check will start and end.
        /// Throws an ArgumentNullException if file is null.
        /// Throws an ArgumentOutOfRangeException if the range [start, start + count) is not a valid range within the current file content.
        /// </summary>
        internal static Range GetSyntaxCheckDelimiters(this FileContentManager file, int start, int count)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (start < 0 || start >= file.NrLines())
            {
                throw new ArgumentOutOfRangeException(nameof(start));
            }
            if (count < 0 || start + count > file.NrLines())
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            // if no piece of code exists before the start of the modifications, then the check effectively starts at the beginning of the file
            var syntaxCheckStart = file.PositionAfterPrevious(new Position(start, 0)); // position (0,0) if there is no previous fragment
            // if the modification goes past what is currently the last piece of code, then the effectively the check extends to the end of the file
            var firstAfterModified = new Position(start + count, 0);
            var lastInFile         = LastInFile(file);
            var syntaxCheckEnd     = firstAfterModified.IsSmallerThan(lastInFile)
                ? file.FragmentEnd(ref firstAfterModified)
                : file.End();

            return(new Range {
                Start = syntaxCheckStart, End = lastInFile.IsSmallerThanOrEqualTo(syntaxCheckEnd) ? file.End() : syntaxCheckEnd
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Extracts the code fragments based on the current file content that need to be re-processed due to content changes on the given lines.
        /// Ignores any whitespace or comments at the beginning of the file (whether they have changed or not).
        /// Ignores any whitespace or comments that occur after the last piece of code in the file.
        /// Throws an ArgumentNullException if any of the arguments is null.
        /// </summary>
        private static IEnumerable <CodeFragment> FragmentsToProcess(this FileContentManager file, SortedSet <int> changedLines)
        {
            // NOTE: I suggest not to touch this routine unless absolutely necessary...(things *will* break)
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (changedLines == null)
            {
                throw new ArgumentNullException(nameof(changedLines));
            }

            var iter       = changedLines.GetEnumerator();
            var lastInFile = LastInFile(file);

            Position processed = new Position(0, 0);

            while (iter.MoveNext())
            {
                QsCompilerError.Verify(0 <= iter.Current && iter.Current < file.NrLines(), "index out of range for changed line");
                if (processed.Line < iter.Current)
                {
                    var statementStart = file.PositionAfterPrevious(new Position(iter.Current, 0));
                    if (processed.IsSmallerThan(statementStart))
                    {
                        processed = statementStart;
                    }
                }

                while (processed.Line <= iter.Current && processed.IsSmallerThan(lastInFile))
                {
                    processed = processed.Copy(); // because we don't want to modify the ending of the previous code fragment ...
                    var nextEnding     = file.FragmentEnd(ref processed);
                    var extractedPiece = file.GetCodeSnippet(new Range {
                        Start = processed, End = nextEnding
                    });

                    // constructing the CodeFragment -
                    // NOTE: its Range.End is the position of the delimiting char (if such a char exists), i.e. the position right after Code ends

                    if (extractedPiece.Length > 0) // length = 0 can occur e.g. if the last piece of code in the file does not terminate with a statement ending
                    {
                        var code = file.GetLine(nextEnding.Line).ExcessBracketPositions.Contains(nextEnding.Character - 1)
                            ? extractedPiece.Substring(0, extractedPiece.Length - 1)
                            : extractedPiece;
                        if (code.Length == 0 || !CodeFragment.DelimitingChars.Contains(code.Last()))
                        {
                            code = $"{code}{CodeFragment.MissingDelimiter}";
                        }

                        var endChar   = nextEnding.Character - (extractedPiece.Length - code.Length) - 1;
                        var codeRange = new Range {
                            Start = processed, End = new Position(nextEnding.Line, endChar)
                        };
                        yield return(new CodeFragment(file.IndentationAt(codeRange.Start), codeRange, code.Substring(0, code.Length - 1), code.Last()));
                    }
                    processed = nextEnding;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the position of the delimiting character that follows the given code fragment.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown when the code fragment has a missing delimiter.</exception>
        private static Position GetDelimiterPosition(FileContentManager file, CodeFragment fragment)
        {
            if (fragment.FollowedBy == CodeFragment.MissingDelimiter)
            {
                throw new ArgumentException("Code fragment has a missing delimiter", nameof(fragment));
            }
            var end      = fragment.Range.End;
            var position = file.FragmentEnd(ref end);

            return(Position.Create(position.Line, position.Column - 1));
        }
        /// <summary>
        /// Returns the position of the delimiting character that follows the given code fragment.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown when the code fragment has a missing delimiter.</exception>
        /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
        private static Position GetDelimiterPosition(FileContentManager file, CodeFragment fragment)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (fragment == null)
            {
                throw new ArgumentNullException(nameof(fragment));
            }
            if (fragment.FollowedBy == CodeFragment.MissingDelimiter)
            {
                throw new ArgumentException("Code fragment has a missing delimiter", nameof(fragment));
            }

            var end      = fragment.GetRange().End;
            var position = file.FragmentEnd(ref end);

            return(new Position(position.Line, position.Character - 1));
        }