Split PSI file to PSI elements chunks
Answered
Hello.
I'm creating my first IntelliJ plugin and I want to get a file content in PSI format so I can read data from it.
The file I'm trying to read is .yml and look something like this:
functions:
addTodo:
handler: src/addTodo.handler
events:
- httpApi:
path: /
method: post
fetchTodos:
handler: src/fetchTodos.handler
events:
- httpApi:
path: /todos
method: get
fetcTodo:
handler: src/fetchTodo.handler
events:
- httpApi:
path: /todo/{id}
method: get
updateTodo:
handler: src/updateTodo.handler
events:
- httpApi:
path: /todo/{id}
method: put
I would like to get all functions names and know their row number. So far I was only able to get the all files as text using the PsiManager.
PsiManager.getInstance(project).findFile(file);
How can I get the function names and row number?
Please sign in to leave a comment.
Hi Gili,
com.intellij.psi.search.FileTypeIndex.getFiles()
.PsiFile
:PsiManager.getProject(project).findFile(virtualFile)
.PsiFile
, you can iterate over its children PSI elements (PsiFile
is the root in the file PSI structure) usingcom.intellij.psi.util.PsiTreeUtil
.PsiElement.getText()
.Document.getLineNumber(textOffset)
method.