Code Editor CTRL+SPACE not working

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Code Editor CTRL+SPACE not working

Postby aryanarvind » Sat Oct 11, 2014 8:35 am

Hello,

I've spent more than 12 hrs trying to figure out how to get the CTRL+SPACE to work on the code editor with customer list of data.

Code: Select all

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import com.jidesoft.editor.CodeEditor;
import com.jidesoft.editor.ListDataCodeEditorIntelliHints;
import com.jidesoft.editor.caret.CaretEvent;
import com.jidesoft.editor.caret.CaretListener;

public class HintsProvider {

   public static Object[] loadHints(String text, int caretPosition) {
      List<String> values = new ArrayList<String>();
      String temp = text.substring(0, caretPosition - 1);
      temp = temp.substring(temp.lastIndexOf("\n") + 1).trim();
      try {
         Class clazz = Class.forName(temp);
         for (Method method : clazz.getMethods()) {
            values.add(method.getName());
         }
      } catch (ClassNotFoundException e) {

      }
      return values.toArray();
   }

   public static void main(String[] args) {
      final CodeEditor ce = new CodeEditor();
      @SuppressWarnings("unchecked")
      final ListDataCodeEditorIntelliHints hints = new ListDataCodeEditorIntelliHints(
            ce, new ArrayList<String>());
      
      ce.getCaretModel().addCaretListener(new CaretListener() {

         @Override
         public void caretUpdated(CaretEvent caretEvent) {
            try {
               hints.setCompletionList(HintsProvider.loadHints(
                     ce.getText(), ce.getCaretPosition()));
               System.out.println(hints.getCompletionList());
            } catch (Exception e) {

            }
         }
      });
      
      JFrame frame = new JFrame();
      frame.getContentPane().add(ce);
      frame.pack();
      frame.setVisible(true);
   }

}




Could you please let me know whats wrong with this code?
aryanarvind
 
Posts: 97
Joined: Mon May 14, 2007 12:37 pm

Re: Code Editor CTRL+SPACE not working

Postby JIDE Support » Sun Oct 12, 2014 11:02 am

Attached please find the revised code to make your demo working although there will be a lot of more work to get it really working for generic Java code editing.

1. You don't need to use caret listener as in updateHints method, we will provide the context that you can use to find out what hints to provide.
2. Since the hints are used to auto-complete, it must have the same leading characters. That's why in the completion list, you need to add the complete strings, not just the missing part.

Right now if you type in "javax.swing.JLabel." and press ctrl-space, you will see all the methods. You can also see "javax.swing.JLabel.get" then ctrl-space to see all getters.

Code: Select all
import com.jidesoft.editor.CodeEditor;
import com.jidesoft.editor.ListDataCodeEditorIntelliHints;

import javax.swing.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class HintsProvider {

   public static Object[] loadHints(String context) {
      List<String> values = new ArrayList<String>();
      String temp = context.substring(0, context.lastIndexOf("."));
      try {
         Class clazz = Class.forName(temp);
         for (Method method : clazz.getMethods()) {
            values.add(temp + "." + method.getName());
         }
      } catch (ClassNotFoundException e) {

      }
      return values.toArray();
   }

   public static void main(String[] args) {
      final CodeEditor ce = new CodeEditor();
       ListDataCodeEditorIntelliHints hints = new ListDataCodeEditorIntelliHints(
               ce, new ArrayList<String>()) {
           @Override
           public boolean updateHints(Object context) {
               Object[] list = HintsProvider.loadHints("" + context);
               setCompletionList(list);
               return super.updateHints(context);
           }
       };


       JFrame frame = new JFrame();
      frame.getContentPane().add(ce);
      frame.pack();
      frame.setVisible(true);
   }

}
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 36184
Joined: Sun Sep 14, 2003 10:49 am

Re: Code Editor CTRL+SPACE not working

Postby aryanarvind » Mon Oct 13, 2014 7:48 am

Thanks for a quick response.
I see that you had to append the Class to the begining of the values?
Removing that does not display the string in the list box, is there a reason for it?


Code: Select all
import java.awt.Color;
import java.awt.Dimension;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import com.jidesoft.editor.CodeEditor;
import com.jidesoft.editor.ListDataCodeEditorIntelliHints;

public class HintsProvider {

   public static Object[] loadHints(String context) {
      List<String> values = new ArrayList<String>();
      String temp = context.substring(0, context.lastIndexOf("."));
      try {
         getMethods(values, temp);
         if (values.isEmpty()) {
            // Check if it's variable.

         }
         System.out.println(values);
      } catch (ClassNotFoundException e) {

      }
      return values.toArray();
   }

   private static void getMethods(List<String> values, String temp)
         throws ClassNotFoundException {
      Class<?> clazz = Class.forName(temp);
      for (Method method : clazz.getMethods()) {
         StringBuffer sb = new StringBuffer();
         // sb.append(temp);
         // sb.append(".");
         sb.append(method.getName());
         sb.append("(");
         int i = 0;
         for (Class<?> type : method.getParameterTypes()) {
            if (i != 0) {
               sb.append(", ");
            }
            sb.append(type.getSimpleName() + "arg" + i++);
         }
         sb.append(")");
         values.add(sb.toString());
      }
   }

   public static void main(String[] args) {
      final CodeEditor ce = new CodeEditor();
      @SuppressWarnings({ "rawtypes", "unchecked" })
      ListDataCodeEditorIntelliHints hints = new ListDataCodeEditorIntelliHints(
            ce, new ArrayList<String>()) {
         @Override
         public boolean updateHints(Object context) {
            Object[] list = HintsProvider.loadHints("" + context);
            setCompletionList(list);
            return super.updateHints(context);
         }
      };

      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      final JScrollPane codeEditorScrollPane = new JScrollPane(ce);

      codeEditorScrollPane
            .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      codeEditorScrollPane
            .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
      codeEditorScrollPane.setBorder(BorderFactory
            .createLineBorder(Color.DARK_GRAY));

      frame.getContentPane().add(codeEditorScrollPane);
      frame.setSize(new Dimension(300,350));
      
      frame.setVisible(true);
   }

}
aryanarvind
 
Posts: 97
Joined: Mon May 14, 2007 12:37 pm

Re: Code Editor CTRL+SPACE not working

Postby JIDE Support » Mon Oct 13, 2014 8:20 am

Auto completion is to auto complete the rest based on the existing content. We will actually do a match to filter the hints you provided. If the beginning of the hints don't match with the context, we will filter them out. That's why you have to provide the complete strings, not just the portion to be completed. If you don't like the hint popup to show the complete strings, you can provide a custom list cell renderer to remove them.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 36184
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 2 guests

cron