Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increased line height for Consolas font in Eclipse

Many people are using Consolas for their primary programming font but unfortunately there is no way to change line height in Eclipse so it looks kinda ugly as it is shown below:

enter image description here

I was wondering if there is anyone who solved this by adding some extra space between lines or simply changing the font itself which has longer height now.

It would be nice to share it with us here on Stackoverflow.

There are some topics I've found while searching for this but none of them were what I am looking for:

  1. How can I change line height / line spacing in Eclipse?
  2. https://stackoverflow.com/questions/15153938/improved-line-spacing-for-eclipse?lq=1
  3. and so on...

Some of them designed their own fonts (such as Meslo Font) by modifying the existing ones so it would be nice if you could share your modified Consolas font.

like image 465
Tarik Avatar asked Dec 03 '13 07:12

Tarik


People also ask

How do I change lines in Eclipse?

To set the maximum line length in Eclipse, open Eclipse and in the menu bar, go to Window -> Preferences and in the pop-up window, go to Java -> Code Style -> Formatter. This will open up the Formatter options in the right window pane. Click on the Edit button to modify the current formatter profile.


1 Answers

As mentioned in one of the answers you reference the underlying StyledText control does have a setLineSpacing method, but the existing editors do not use it.

The CSS styling code in Eclipse 4.3 does provide a way to access this but it requires writing a plugin to extend the CSS in order to do so.

The plugin.xml for the plugin would look like this:

<plugin>
   <extension
         point="org.eclipse.e4.ui.css.core.elementProvider">
      <provider
            class="linespacing.LineSpacingElementProvider">
         <widget
               class="org.eclipse.swt.custom.StyledText"></widget>
      </provider>
   </extension>
   <extension
         point="org.eclipse.e4.ui.css.core.propertyHandler">
      <handler
            adapter="linespacing.StyledTextElement"
            composite="false"
            handler="linespacing.LineSpacingPropertyHandler">
         <property-name
               name="line-spacing">
         </property-name>
      </handler>
   </extension>
</plugin>

which declares a CSS element provider LineSpacingElementProvider which would be:

public class LineSpacingElementProvider implements IElementProvider
{
  @Override
  public Element getElement(final Object element, final CSSEngine engine)
  {
    if (element instanceof StyledText)
      return new StyledTextElement((StyledText)element, engine);

    return null;
  }
}

The StyledTextElement this provides is just:

public class StyledTextElement extends ControlElement
{
  public StyledTextElement(StyledText control, CSSEngine theEngine)
  {
    super(control, theEngine);
  }
}

The second declaration in the plugin.xml is a CSS property handler for a property called line-spacing

public class LineSpacingPropertyHandler extends AbstractCSSPropertySWTHandler implements ICSSPropertyHandler
{
  @Override
  protected void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception
  {
    if (!(control instanceof StyledText))
      return;

    StyledText text = (StyledText)control;

    if ("line-spacing".equals(property))
     {
       int pixelValue = (int)((CSSPrimitiveValue)value).getFloatValue(CSSPrimitiveValue.CSS_PX);

       text.setLineSpacing(pixelValue);
     }
  }

  @Override
  protected String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception
  {
     return null;
  }
}

With a plugin containing this installed you can then modify one of the existing CSS style sheets to contain:

StyledText {
    line-spacing: 2px;
}
like image 177
greg-449 Avatar answered Oct 07 '22 17:10

greg-449