Represents a character position within the document.
        public bool GetFuncSigName(TextLocation currLoc)
        {
            _source.Clear();

            var app = ProbeNppPlugin.Instance;
            var lineNum = currLoc.Line;
            var lineText = app.GetText(app.GetLineStartPos(currLoc.Line), currLoc);
            _source.Append(RemoveComments(lineText, app.GetLineState(lineNum)));

            while (true)
            {
                switch (ParseFunctionSigFromSource())
                {
                    case ParseState.Found:
                        return true;
                    case ParseState.BadSyntax:
                        return false;
                    default:	// ParseState.NotEnoughSource:
                        lineNum--;
                        if (lineNum < 1) return false;
                        _source.AppendLine(RemoveComments(app.GetLineText(lineNum), app.GetLineState(lineNum)));
                        break;
                }
            }
        }
Beispiel #2
0
        public void TranslateToSnapshot(ref TextLocation point)
        {
            foreach (var edit in _edits)
            {
                if (edit.startLoc > point)
                {
                    // Edit occurred after the point, so the position is not affected.
                }
                else if (edit.endLoc.Line < point.Line)
                {
                    // Occurred on a line prior to this one.
                    var line = point.Line;
                    if (edit.insert) line -= edit.endLoc.Line - edit.startLoc.Line;
                    else line += edit.endLoc.Line - edit.startLoc.Line;
                    point.Line = line <= 0 ? 1 : line;
                    continue;
                }
                else if (edit.endLoc < point)
                {
                    // Edit affects this line, but before the point.

                    var line = point.Line;
                    var charPos = point.CharPosition;
                    if (edit.insert)
                    {
                        line -= edit.endLoc.Line - edit.startLoc.Line;
                        charPos = point.CharPosition - (edit.endLoc.CharPosition - 1) + (edit.startLoc.CharPosition - 1);
                    }
                    else // delete
                    {
                        line += edit.endLoc.Line - edit.startLoc.Line;
                        charPos = point.CharPosition + (edit.endLoc.CharPosition - 1) - (edit.startLoc.CharPosition - 1);
                    }
                    point.Line = line <= 0 ? 1 : line;
                    point.CharPosition = charPos <= 0 ? 1 : charPos;
                }
                else
                {
                    // Edit surrounds the point.
                    if (edit.insert) point = edit.startLoc;
                    else point = edit.endLoc;
                }
            }
        }
Beispiel #3
0
 public void Modify(TextLocation location, string text, bool insert)
 {
     var lineEndIndex = text.LastIndexOf('\n');
     var edit = lineEndIndex < 0 ?
         new Edit
         {
             insert = insert,
             startLoc = location,
             endLoc = new TextLocation(location.Line, location.CharPosition + text.Length)
         }
         :
         new Edit
         {
             insert = insert,
             startLoc = location,
             endLoc = new TextLocation(location.Line + text.Count(c => c == '\n'), text.Length - lineEndIndex + 1)
         };
     _edits.Insert(0, edit);
 }
        public bool IsAutoCompletionAllowedHere(TextLocation location)
        {
            var app = ProbeNppPlugin.Instance;
            var currentLine = app.CurrentLine;
            var lineText = app.GetLineText(currentLine);
            var state = currentLine > 1 ? app.GetLineState(currentLine - 1) : 0;

            var startPos = 0;
            if ((state & ProbeLexer.State_InsideComment) != 0)
            {
                var index = lineText.IndexOf("*/");
                if (index < 0) return false;
                startPos = index + 2;
            }

            if (startPos + 1 > location.CharPosition) return false;

            var parser = new TokenParser.Parser(app.GetLineText(currentLine));
            parser.ReturnComments = true;
            parser.ReturnWhiteSpace = true;
            if (startPos > 0) parser.SetOffset(startPos);

            while (parser.Read())
            {
                if (parser.Position.LinePos > location.CharPosition || parser.EndOfFile)
                {
                    if (parser.TokenType == TokenParser.TokenType.Comment || parser.TokenType == TokenParser.TokenType.StringLiteral)
                    {
                        return false;
                    }
                    return true;
                }
            }

            if (parser.TokenType == TokenParser.TokenType.Comment || parser.TokenType == TokenParser.TokenType.StringLiteral)
            {
                return false;
            }
            return true;
        }
Beispiel #5
0
		private bool TryFindOpeningBrace(TextLocation closeBraceLoc, out TextLocation openBraceLoc)
		{
			var app = ProbeNppPlugin.Instance;
			var source = app.GetText(app.Start, app.CurrentLocation);
			var sourceLength = source.Length;
			var found = false;
			var openLoc = closeBraceLoc;

			var parser = new TokenParser.Parser(source);
			while (parser.ReadNestable())
			{
				if (parser.TokenType == TokenParser.TokenType.Nested &&
					parser.TokenText.EndsWith("}") &&
					parser.Position.Offset == sourceLength)
				{
					openLoc = parser.TokenStartPostion.ToNppSharpTextLocation();
					found = true;
				}
			}

			if (found)
			{
				openBraceLoc = openLoc;
				return true;
			}
			else
			{
				openBraceLoc = closeBraceLoc;
				return false;
			}
		}
        public void OnCharAdded(CharAddedEventArgs e)
        {
            var app = ProbeNppPlugin.Instance;

            if (!app.Settings.Editor.AutoCompletion) return;
            if (!IsAutoCompletionAllowedHere(app.CurrentLocation)) return;

            if (e.Character == '.')
            {
                var wordEnd = app.CurrentLocation - 1;
                var wordStart = app.GetWordStartPos(wordEnd, false);
                var word = app.GetText(wordStart, wordEnd);

                if (!string.IsNullOrWhiteSpace(word))
                {
                    var table = ProbeEnvironment.GetTable(word);
                    if (table == null) return;

                    var fields = table.Fields;
                    if (!fields.Any()) return;
                    app.ShowAutoCompletion(0, (from f in fields orderby f.Name.ToLower() select f.Name), true);
                }
            }
            else if (char.IsLetterOrDigit(e.Character))
            {
                if (!app.AutoCompletionIsActive)
                {
                    var lineText = app.GetText(app.GetLineStartPos(app.CurrentLine), app.CurrentLocation);

                    Match match;
                    ProbeTable table;
                    if ((match = _rxTableField.Match(lineText)).Success &&
                        (table = ProbeEnvironment.GetTable(match.Groups[1].Value)) != null)
                    {
                        var field = match.Groups[2].Value;
                        app.ShowAutoCompletion(field.Length, (from f in table.Fields orderby f.Name select f.Name), true);
                    }
                    else if ((match = _rxAutoCompleteWord.Match(lineText)).Success)
                    {
                        var word = match.Value;

                        var model = app.CurrentModel;
                        if (model != null)
                        {
                            app.ShowAutoCompletion(word.Length, GetSoloAutoCompletionItems(app.CurrentLocation, word));
                        }
                    }
                }
            }
            else if (e.Character == '(')
            {
                if (!app.FunctionSignatureIsActive)
                {
                    var lineText = app.GetText(app.GetLineStartPos(app.CurrentLine), app.CurrentLocation);

                    Match match;
                    if ((match = _rxFuncCall.Match(lineText)).Success)
                    {
                        var funcName = match.Groups[1].Value;

                        var entered = match.Length - (match.Groups[1].Index - match.Index);
                        var callTipPos = new TextLocation(app.CurrentLine, app.CurrentLocation.CharPosition - entered);

                        var funcSig = GetFunctionSignature(funcName);
                        if (!string.IsNullOrEmpty(funcSig))
                        {
                            app.ShowFunctionSignature(callTipPos, funcSig);

                            int highlightStart, highlightLength;
                            if (GetFunctionSignatureHighlightRange(funcSig, 0, out highlightStart, out highlightLength))
                            {
                                app.SetFunctionSignatureHighlight(highlightStart, highlightLength);
                            }
                        }
                    }
                }
            }
            else if (e.Character == ',')
            {
                var sigParser = new FunctionSignatureParser();
                if (sigParser.GetFuncSigName(app.CurrentLocation))
                {
                    var funcSig = GetFunctionSignature(sigParser.FunctionName);
                    if (!string.IsNullOrEmpty(funcSig))
                    {
                        app.ShowFunctionSignature(app.CurrentLocation, funcSig);

                        int highlightStart, highlightLength;
                        if (GetFunctionSignatureHighlightRange(funcSig, sigParser.CommaCount, out highlightStart, out highlightLength))
                        {
                            app.SetFunctionSignatureHighlight(highlightStart, highlightLength);
                        }
                    }
                }
            }
            else if (e.Character == ')')
            {
                if (app.FunctionSignatureIsActive) app.CancelFunctionSignature();
            }
        }
        private IEnumerable<string> GetSoloAutoCompletionItems(TextLocation location, string startsWith)
        {
            var app = ProbeNppPlugin.Instance;

            var model = app.CurrentModel;
            if (model != null)
            {
                var list = new SortedSet<string>();

                var modelLoc = location;
                model.Tracker.TranslateToSnapshot(ref modelLoc);
                var pos = model.GetPosition(modelLoc);

                foreach (var item in (from i in model.GetAutoCompletionItems(pos) where i.Text.StartsWith(startsWith) select i.Text))
                {
                    list.Add(item);
                }

                foreach (var item in (from t in ProbeEnvironment.AutoCompletionTables where t.Text.StartsWith(startsWith) select t.Text))
                {
                    list.Add(item);
                }

                if (app.LanguageName == Res.ProbeSourceLanguageName)
                {
                    foreach (var item in (from k in app.SourceKeywords where k.StartsWith(startsWith) select k))
                    {
                        list.Add(item);
                    }
                }
                else if (app.LanguageName == Res.ProbeDictLanguageName)
                {
                    foreach (var item in (from k in app.DictKeywords where k.StartsWith(startsWith) select k))
                    {
                        list.Add(item);
                    }
                }

                foreach (var item in (from f in app.FunctionSignatures.Keys where f.StartsWith(startsWith) select f))
                {
                    list.Add(item);
                }

                var funcScanner = app.FunctionFileScanner;
                if (funcScanner != null)
                {
                    foreach (var item in (from f in funcScanner.GetFunctionSignatures(startsWith) select f.Name))
                    {
                        list.Add(item);
                    }
                }

                foreach (var item in (from k in app.DataTypes where k.StartsWith(startsWith) select k))
                {
                    list.Add(item);
                }

                //list.Sort((a, b) => string.Compare(a, b, true));
                return list;
            }

            return new string[0];
        }
Beispiel #8
0
 /// <summary>
 /// Copies the specified text range into the clipboard.
 /// </summary>
 /// <param name="start">The starting position.</param>
 /// <param name="end">The ending position (exclusive).</param>
 public void Copy(TextLocation start, TextLocation end)
 {
     Plugin.NppIntf.Copy(start, end);
 }
Beispiel #9
0
 private TextLocation GetIndentPosOnLine(int line)
 {
     var pos = new TextLocation(line, 1);
     while (true)
     {
         var ch = GetText(pos, 1);
         if (string.IsNullOrEmpty(ch)) break;
         if (!Char.IsWhiteSpace(ch[0])) return pos;
         pos++;
     }
     return new TextLocation(line, 1);
 }
Beispiel #10
0
 /// <summary>
 /// Removes the selection and sets the caret at pos.
 /// (The caret is not scrolled into view)
 /// </summary>
 /// <param name="pos">The new start/end position for the selection.</param>
 public void SetEmptySelection(TextLocation pos)
 {
     Plugin.NppIntf.SetEmptySelection(pos);
 }
Beispiel #11
0
 /// <summary>
 /// Returns the x and y display location of the position.
 /// </summary>
 /// <param name="pos">The position to find.</param>
 /// <returns>The client coordinates of the text position.</returns>
 public Point PosToPoint(TextLocation pos)
 {
     return Plugin.NppIntf.PosToPoint(pos.ByteOffset);
 }
Beispiel #12
0
 /// <summary>
 /// Finds the position closest to a point on the screen.
 /// </summary>
 /// <param name="pt">The client coordinates of the point to test.</param>
 /// <param name="location">An out parameter to receive the found location.</param>
 /// <returns>
 /// If no character is close, or the point is outside the window, this function returns
 /// false, and the location parameter is set to the start of the document.
 /// If a location was found, this function returns true, and the location parameter is set
 /// to the close position.
 /// </returns>
 public bool PointToPosClose(Point pt, out TextLocation location)
 {
     int offset = Plugin.NppIntf.PointToPosClose(pt);
     if (offset < 0)
     {
         location = TextLocation.Start;
         return false;
     }
     location = TextLocation.FromByteOffset(offset);
     return true;
 }
Beispiel #13
0
 /// <summary>
 /// Goes to the specified position and ensures it is visible.
 /// </summary>
 /// <param name="pos">The position to go to.</param>
 public void GoTo(TextLocation pos)
 {
     Plugin.NppIntf.GoToPos(pos);
 }
Beispiel #14
0
 /// <summary>
 /// Gets the position at the start of the current word.
 /// </summary>
 /// <param name="pos">The starting position.</param>
 /// <param name="onlyWordChars">If true, only word characters will be jumped.
 /// If false, all characters will be jumped.</param>
 /// <returns>The position at the start of the word.</returns>
 public TextLocation GetWordStartPos(TextLocation pos, bool onlyWordChars)
 {
     return Plugin.NppIntf.GetWordStartPos(pos, onlyWordChars);
 }
Beispiel #15
0
 /// <summary>
 /// Gets the text for the specified range.
 /// </summary>
 /// <param name="start">The starting position.</param>
 /// <param name="end">The ending position (exclusive).</param>
 /// <returns>The text for the given range.</returns>
 public string GetText(TextLocation start, TextLocation end)
 {
     return Plugin.NppIntf.GetText(start, end);
 }
Beispiel #16
0
 /// <summary>
 /// Gets the text for the specified range.
 /// </summary>
 /// <param name="start">The starting position.</param>
 /// <param name="numChars">The number of characters to retrieve.</param>
 /// <returns>The text for the given range.</returns>
 public string GetText(TextLocation start, int numChars)
 {
     return Plugin.NppIntf.GetText(start, numChars);
 }
Beispiel #17
0
 /// <summary>
 /// Sets the selection range.
 /// (The caret is scrolled into view)
 /// </summary>
 /// <param name="anchorPos">The selection anchor position.</param>
 /// <param name="currentPos">The selection current position.</param>
 public void SetSelection(TextLocation anchorPos, TextLocation currentPos)
 {
     Plugin.NppIntf.SetSelection(anchorPos, currentPos);
 }
Beispiel #18
0
 /// <summary>
 /// Copies a text location object.
 /// </summary>
 /// <param name="c">The location to be copied.</param>
 public TextLocation(TextLocation c)
 {
     _line = c._line;
     _ch = c._ch;
 }
Beispiel #19
0
 /// <summary>
 /// Creates the event arguments object.
 /// </summary>
 /// <param name="type">The type of modification that occurred.</param>
 /// <param name="text">The text inserted or deleted.</param>
 /// <param name="location">The location of the modification.</param>
 /// <param name="userAction">A flag indicating if this action was caused by the user.</param>
 /// <param name="undo">A flag indicating if this action is part of an undo process.</param>
 /// <param name="redo">A flag indicating if this action is part of a redo process.</param>
 /// <param name="linesAdded">The number of lines added by this modification.</param>
 public ModifiedEventArgs(ModificationType type, TextPtr text, TextLocation location, bool userAction, bool undo, bool redo, int linesAdded)
 {
     ModificationType = type;
     Text = text;
     Location = location;
     UserAction = userAction;
     Undo = undo;
     Redo = redo;
     LinesAdded = linesAdded;
 }
Beispiel #20
0
 /// <summary>
 /// Copies the specified text range into the clipboard.
 /// </summary>
 /// <param name="start">The starting position.</param>
 /// <param name="numChars">The number of characters to be copied.</param>
 public void Copy(TextLocation start, int numChars)
 {
     Plugin.NppIntf.Copy(start, numChars);
 }