Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java8 Formatting With Block Comments #181

Merged
merged 9 commits into from
Jun 23, 2019
Merged
24 changes: 16 additions & 8 deletions src/core/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,22 @@ impl<'parse, Symbol: GrammarSymbol + 'parse> FormatJob<'parse, Symbol> {
let mut postfix = String::new();

if let Some(injections) = injections_opt {
for injection in injections.iter().rev() {
let injection_string = self.injection_string(injection, outer_scope);

match injection.direction {
InjectionAffinity::Left => postfix = format!("{}{}", postfix, injection_string),
InjectionAffinity::Right => prefix = format!("{}{}", injection_string, prefix),
}
}
injections
.iter()
.filter(|injection| injection.direction == InjectionAffinity::Left)
.for_each(|injection| {
let injection_string = self.injection_string(injection, outer_scope);
postfix = format!("{}{}", postfix, injection_string);
});

injections
.iter()
.filter(|injection| injection.direction == InjectionAffinity::Right)
.rev()
.for_each(|injection| {
let injection_string = self.injection_string(injection, outer_scope);
prefix = format!("{}{}", injection_string, prefix);
});
}

format!("{}{}{}", prefix, child_string, postfix)
Expand Down
235 changes: 235 additions & 0 deletions tests/input/java8_block_comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/* Here is where the license goes
* and here
* and here
...
...
...
...
...
...
...
...
...
...
and now its done.*/
package com.konjex.util;

/* Something */
import org.jetbrains.annotations.NotNull; /* Why is there a comment here? */

import java.util.Iterator;

/**
Here is some comment about the class
It does x, y, z.
* For more information see https://www.moreinfo.com/? */
public class DoublyLinkedList<T> implements Iterable<T> {

/* Start of the linked list
*
*/
private LinkedListNode<T> first;
private LinkedListNode<T> last; /* End of the linked list */
private int length;

/* Default constructor */
public DoublyLinkedList(){
/* No-op */
}

public DoublyLinkedList(){
/*
No-op
*/
}

public DoublyLinkedList(){/*
* No-op
*/
}

public DoublyLinkedList(){
/* No-op
*/
}


public DoublyLinkedList(){
/*
No-op
*/

/* HERE
* BE
THERE
...
EVERYWHERE*/
}

public DoublyLinkedList(){/*
No-op
*/

/* HERE
* BE
THERE
...
EVERYWHERE*//*
BUT WAIT!
THERE ARE MORE COMMENTS!!!
*/
}

public DoublyLinkedList(){

/*
* This lone isolated comment should be compacted!
*/

}

public DoublyLinkedList(){

/* This lone isolated comment should be compacted! */

}

public DoublyLinkedList(
T... elements /* This is a strange place for a comment
*/){
/* Just add all the elements */
for(T element : elements){
addLast(element);
}
}

public void addFirst(T value){
if(isEmpty()){
first = new LinkedListNode<>(value, null, null);
last = first;
}
else{
LinkedListNode<T> newNode = new LinkedListNode<>(value, null, first);
first.setPrev(newNode); /** Append to the front
* Then do something else
Then do a third thing
*/
first = newNode;
}
length ++;
}

/*
* Add an element to the end of the linked list.
* Never throws.
*/
public void addLast(T value){
if(isEmpty()){
first = new LinkedListNode<>(value, null, null);
last = first;
}
/*
* Why would there be a comment between these??
*/
else{
LinkedListNode<T> newNode = new LinkedListNode<>(value, last, null);
last.setNext(newNode);
last = newNode;
}
length ++;
}

public void removeFirst(){
if(!removeIfLast()){
first = first.getNext();
first.setPrev(null);
}
}

public void removeLast(){
if(!removeIfLast()){
last = last.getPrev();
last.setNext(null);
}
}

/* ----------------------------------------------------------------------
HERE IS A LARGE FLOATING COMMENT
---------------------------------------------------------------------- */

private boolean removeIfLast(){


/* Here is a small floating comment */

if(isEmpty()){
return true;
}
/* Decrement length */
length --;
/* Reset first and last pointers if the list is now empty.
Return true if this is the case.

*/
if(length == 0){
first = null;
last = null;
return true;
}
return false; /*

Return false only if there are remaining elements */
}

public boolean isEmpty(){
return first == null; /*
* Return true if there is no first element */
}

public LinkedListNode<T> getFirst(){
return first;
}

public LinkedListNode<T> getLast(){
return last;
}

public int size(){
return length;
}

@NotNull
@Override
public Iterator<T> iterator(){
return new Iterator<T>(){ /* A */
private LinkedListNode<T> currentNode = first;

@Override
public boolean hasNext() {
/* Test1 */
/* Test2 */
return !isEmpty() && currentNode.hasNext();
}

@Override
public T next() {
T value = currentNode /* This is a strange place for a comment */
.getValue();
currentNode = currentNode
.getNext() /* This place is even stranger */
;
return value;
}

@Override /* This method overrides a method on Iterator

*/
public void remove() {
/*
* We don't allow element removal via this iterator
*/
throw new UnsupportedOperationException();
}
};
}
}
2 changes: 1 addition & 1 deletion tests/input/java8_interface
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.konjex.util.provide;

/**
/*
* Object that can be provided by a provider.
*/
public interface Providable<MatchType> extends DataClass {
Expand Down
4 changes: 2 additions & 2 deletions tests/input/java8_medium
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.konjex.lens.conf;
import java.io.File;
import java.util.function.Consumer;

/**
/*
* Class for loading lens configuration files by name.
*/
public abstract class ConfigFileLoader {
Expand Down Expand Up @@ -56,4 +56,4 @@ public abstract class ConfigFileLoader {

}

}
}
Loading