locked
How to get the first column of a line? RRS feed

  • Question

  • I get get the last column in a line using IVsTextLines.GetLengthOfLine method. However, what can I use to get the first column in line (the first column that isn't whitespace)? Should I just go parse the text to find that out, or is there a better way?

    Thanks!

    Thursday, February 24, 2011 9:05 PM

Answers

  • Hi VitalyBe,

    Thanks for your feedback.

    I got wo know your issue better now,

    your method is good and alternatively, you can try to get the string of the specific line with

    GetLineText

    get the length of the string, and remove the leading white space with

    TrimStart Method

    and compare the length of string left.

    Hope my reply helps.

     

     

    Best Regards,

    Ziwei Chen

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by VitalyB Saturday, March 5, 2011 12:13 PM
    • Marked as answer by Victor_Chen Monday, March 7, 2011 2:31 AM
    Tuesday, March 1, 2011 5:29 AM

All replies

  • Hi VitalyBe,

    Thanks for your post.

    I suppose you get the last column by the obtained length pointer by IVsTextLines.GetLengthOfLine,

    if so, you can try IVsTextBuffer.GetPositionOfLineIndex Method to get the first column.

    If I misunderstand your issue, be free to let me know.

     

     

    Best Regards,

    Ziwei Chen

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 28, 2011 3:29 AM
  • Hi Victor,

    The function GetPositionOfLineIndex converts line/column to a position... How can I use it to get the first column in a specific file?

    I wrote this to find the first column, just not sure how efficient it is:

        /// <summary>
        /// Returns the first non-whitespace column of the line
        /// </summary>
        /// <returns>1-based first non-whitespace column</returns>
        public static int GetFirstColumnOfLine1Based(this IVsTextLines textLines, int line)
        {
          Release.Assert(line > 0, "1-based line must be bigger than 0");
    
          int lastColumn = textLines.GetLastColumnOfLine1Based(line);
    
          string lineText;
          textLines.GetLineText(line - 1, 0, line-1, lastColumn, out lineText);
          int firstColumn = lineText.FirstIndexWhere(c => c != ' ' && c != '\t');
    
          return firstColumn+1;
        }
    

    Monday, February 28, 2011 9:47 AM
  • Hi VitalyBe,

    Thanks for your feedback.

    I got wo know your issue better now,

    your method is good and alternatively, you can try to get the string of the specific line with

    GetLineText

    get the length of the string, and remove the leading white space with

    TrimStart Method

    and compare the length of string left.

    Hope my reply helps.

     

     

    Best Regards,

    Ziwei Chen

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by VitalyB Saturday, March 5, 2011 12:13 PM
    • Marked as answer by Victor_Chen Monday, March 7, 2011 2:31 AM
    Tuesday, March 1, 2011 5:29 AM
  • Hi VitalyBe,

     

    Is it resolved?


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 4, 2011 9:16 AM
  • Resolved and marked as such. Thanks!
    Saturday, March 5, 2011 12:13 PM