Skip to content

Commit

Permalink
Merge pull request #9 from mwillema/issue-9
Browse files Browse the repository at this point in the history
Bad auto indent of closing bracket for embedded message
  • Loading branch information
Marco Willemart authored Feb 1, 2017
2 parents 5421213 + 1b412b4 commit 4112860
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 102 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# Protobuf NetBeans Plugin
Netbeans IDE plugin to support Protocol Buffers

## Installation

### Installation via NetBeans Update Center
The plugin is available at [http://plugins.netbeans.org/plugin/70658/](http://plugins.netbeans.org/plugin/70658/), and can be installed via the NetBeans Update Center automatically.

### Manual Installation
Make sure you don't have an old version installed.

1. Download the [latest release](http://plugins.netbeans.org/plugin/70658/)
3. Start Netbeans
4. Select Tools -> Plugins -> Downloaded -> Add Plugins...
5. Select the .nbm file
6. Accept the license and the installation of self-signed plugins
2 changes: 1 addition & 1 deletion protobuf-editor/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ AutoUpdate-Show-In-Client: true
OpenIDE-Module: com.marcowillemart.protobuf.editor
OpenIDE-Module-Layer: com/marcowillemart/protobuf/editor/layer.xml
OpenIDE-Module-Localizing-Bundle: com/marcowillemart/protobuf/editor/Bundle.properties
OpenIDE-Module-Specification-Version: 1.0.0
OpenIDE-Module-Specification-Version: 1.0.1
6 changes: 6 additions & 0 deletions protobuf-editor/src/com/marcowillemart/common/lang/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public interface Line {
*/
String text();

/**
* @return the indentation of this, i.e., the whitespaces in this.text
* before the first non-white character.
*/
String indentation();

/**
* @return true iff the last non-white character of this.text is a '{'
*/
Expand Down
13 changes: 13 additions & 0 deletions protobuf-editor/src/com/marcowillemart/common/lang/SimpleLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public String text() {
return text;
}

@Override
public String indentation() {
int i;

for (i = 0; i < text.length(); i++) {
if (!Character.isWhitespace(text.charAt(i))) {
break;
}
}

return text.substring(0, i);
}

@Override
public boolean isLastOpeningBrace() {
for (int i = text.length() - 1; i >= 0; i--) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean beforeInsert(Context context) throws BadLocationException {
public void insert(MutableContext context) throws BadLocationException {
Document document = documentFrom(context);

addExtraLineBetweenBraces(context);
addExtraLineBetweenBraces(context, document);
closeBlockComment(context, document);
addLineToBlockComment(context, document);
}
Expand All @@ -55,19 +55,24 @@ public void cancelled(Context context) {
////////////////////

/**
* @requires context != null
* @requires context != null && document != null
* @modifies context
* @effects Adds an extra line if the line break is typed between two
* matching braces.
*/
private static void addExtraLineBetweenBraces(MutableContext context) {
private static void addExtraLineBetweenBraces(
MutableContext context,
Document document) {

int offset = context.getBreakInsertOffset();

if (0 < offset && offset < context.getDocument().getLength()) {
String surroundingchars = surroundingCharacters(context, offset);

if (PAIR_OF_CURLY_BRACES.equals(surroundingchars)) {
context.setText("\n\n", 1, 1);
String indentation = document.lineAt(offset).indentation();

context.setText(String.format("\n\n%s", indentation), 1, 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.marcowillemart.common.lang;

import static org.junit.Assert.*;
import org.junit.Test;

/**
* Unit tests for the SimpleLine class.
*
* @author mwi
*/
public class SimpleLineTest {

private Line target;

@Test
public void testIndentation_none() {
// Setup
target = createLine("message M {}");

// Exercise & Verify
assertTrue(target.indentation().isEmpty());
}

@Test
public void testIndentation_space() {
// Setup
target = createLine(" message M {}");

// Exercise & Verify
assertEquals(" ", target.indentation());
}

@Test
public void testIndentation_tab() {
// Setup
target = createLine("\tmessage M {}");

// Exercise & Verify
assertEquals("\t", target.indentation());
}

@Test
public void testIndentation_emptyLine() {
// Setup
target = createLine("");

// Exercise & Verify
assertTrue(target.indentation().isEmpty());
}

@Test
public void testIndentation_onlyWhitespaces() {
// Setup
target = createLine(" \t ");

// Exercise & Verify
assertEquals(" \t ", target.indentation());
}

////////////////////
// HELPER METHODS
////////////////////

private static Line createLine(String text) {
return new SimpleLine(1, 0, text.length(), text);
}
}

0 comments on commit 4112860

Please sign in to comment.