Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:07:56+00:00 2026-05-31T01:07:56+00:00

I am working with the selections within the Eclipse source code editor. I defined

  • 0

I am working with the selections within the Eclipse source code editor. I defined a key-binding that gets me the “text” at the current caret position. I managed to parse the current caret position to an IJavaElement

private void processEditorSelection(IEditorPart part, ITextSelection selection) {
          IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
            final ITypeRoot root = (ITypeRoot) JavaUI.getEditorInputJavaElement(editorInput);
            if (root != null) {
               int offset = (selection).getOffset();
                IJavaElement[] codeSelect = root.codeSelect(offset, 0);
                if (codeSelect.length > 0) {
                    //codeSelect[0...n] are my resolved elements at the current caret pos.
       }

What I did not managed to figure out yet is, how to resolve the enclosing type. for instance:

public void do() {
       System.out.println("it"); 
}

If the caret is on the System.out, I obtain an JavaElement of the system out call, but I am actually interested in the enclosing method. Does anyone know how to ask Eclipse for the enclosing type? Like what is the compilation unit or method my current carret position is enclosed in?

Thanks for an answer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:07:58+00:00Added an answer on May 31, 2026 at 1:07 am

    You are looking to do something like this:

    ITypeRoot root = (ITypeRoot) JavaUI.getEditorInputJavaElement(editorInput);
    IJavaElement elt = root.getElementAt(caretPosition);
    

    This will find the enclosing element, which may be an IField, IMethod, or ICompilationUnit. To get the enclosing type, call this:

    IType type = (IType) elt.getAncestor(IJavaElement.TYPE);
    

    This method returns null if there is no enclosing type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an in-browser editor within a textarea . I have started
I've been working on making my own little text editor using a RichTextBox(MyRTB). I've
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project that parses a log of events, and then updates a
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
I copy stuff from output buffers into C++ code I'm working on in vim.
I've got an XIB window that I'm working with in Interface Builder. It has
I am working inside of a quite complex eclipse based application, and having a
I'm trying to implement multiple spinners within one activity, which appears to be working
I am working on a Excel Spreadsheet that when a dropdown box value is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.