A list of functions/methods from source code?

SyntaxEditor for WPF Forum

Posted 9 years ago by Garry Taylor
Version: 15.1.0622
Avatar

Hello all,

I have a SyntaxEditor working very nicely in my application, but I'd like to be able to interrogate it for all function/method definitions. However, I think I'm going about it in the wrong way. Here is basically what I'm doing:

 

 internal List<String> functionList()
        {

            ITextSnapshotReader reader = _codeEditor.Document.CurrentSnapshot.GetReader(0); 



            List<String> items = new List<String>();

            while (reader.GoToNextToken()) {
               IToken tok = reader.ReadToken();
                if (tok != null) {
                    if (tok.Key != null) {
                       if (tok.Key.Equals("Identifier"))
                       {
                           ActiproSoftware.Text.TextRange range = tok.TextRange;
                           String alltext = _codeEditor.Document.CurrentSnapshot.GetText(LineTerminator.Newline);

                           
                           String funcitem = alltext.Substring(range.StartOffset, range.AbsoluteLength);
          

                           items.Add(funcitem);

                       }
                    }
                }
             

            }

            return items;

        }

 

And what I end up with for this simple bit of C:

 

#include <stdio.h>



int func1(void)
{
  return 0;
}

int func2(void)
{
  return 0;
}


int func3(void)
{
  return 0;
}


int func4(void)
{
  return 0;
}

 

Is:

func1

func2

func4

 

So, it's missing func3, and I don't know why. The same thing happens for PHP or Python code, I just seem to be missing one or two function definitions. Using the key 'Identifier' will also give me function calls and variable names, but I can probably work around that. My real problem is that some function definitions are missing.

Am I going about this in really the wrong way? Must I use Antlr instead? I don't need a full-blown lexing of the source code, just function/method definitions, which my above code gets me 95% of the way there, I'm just mysteriously missing definitions for some reason.

Thanks

Garry

Comments (3)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Garry,

First, I would say that calling snapshot.GetText is an "expensive" operation since you are reconstructing the entire text string of the document and it's normally stored in a treelike structure.  You should never really be calling that except when you truly need the whole document text such as for saving a file.

Instead of doing that, you can call: 

var funcitem = _codeEditor.Document.CurrentSnapshot.GetSubstring(tok.TextRange, LineTerminator.Newline); 

That will be far more efficient.

For the issing in missing an identifier, I would recommend that you step through the loop and debug it to see where things are going wrong.  SyntaxEditor should be properly returning all tokens in order, so it could be that you tokenized something incorrectly with your lexer definition or something else going on.  By debugging the loop you should be able to get more info on the problem. 


Actipro Software Support

Posted 9 years ago by Garry Taylor
Avatar

Thanks for the reply, I've put in your suggestion for replacing GetText, and it certainly seems faster, so thanks for that.

My Lexer definitions are the standard langdef files for SyntaxEditor, and the highlighting looks great, but perhaps these .langdef files are not providing enough information to extract function defs?

 

Stepping through the loop, I can see there is a gap in the text ranges where my function definition is, so the the list of tokens just seem to skip it, not sure why that would be.

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Garry,

The list of tokens shouldn't be skipping that range if your lexer is set up properly, especially if the syntax highlighting is correct there since that wouldn't work right if the tokens weren't found. 

If you'd like to have us take a look, please make a new simple sample project that shows the issue and email that to our support address.  In your email, reference this thread and be sure to rename the .zip file extension of what you send so it doesn't get spam blocked.  Thanks!


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.