com.google.code.sbt.compiler.sbt013.SBT013Position Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013-2017 Grzegorz Slowikowski (gslowikowski at gmail dot com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.google.code.sbt.compiler.sbt013;
import java.io.File;
import xsbti.Maybe;
import xsbti.Position;
import com.google.code.sbt.compiler.api.SourcePosition;
/**
* SBT xsbti.Position
* wrapper around {@link SourcePosition} delegate.
*
* @author Grzegorz Slowikowski
*/
public class SBT013Position
implements Position
{
private SourcePosition sourcePosition;
/**
* Creates SBT xsbti.Position
* wrapper around {@link SourcePosition} delegate.
*
* @param sourcePosition {@link SourcePosition} delegate
*/
public SBT013Position( SourcePosition sourcePosition )
{
this.sourcePosition = sourcePosition;
}
@Override
public Maybe line()
{
int line = sourcePosition.getLine();
return line > 0 ? Maybe.just( Integer.valueOf( line ) ) : Maybe.nothing();
}
@Override
public String lineContent()
{
String lineContent = sourcePosition.getLineContent();
return lineContent != null ? lineContent : "";
}
@Override
public Maybe offset()
{
int offset = sourcePosition.getOffset();
return offset >= 0 ? Maybe.just( Integer.valueOf( offset ) ) : Maybe.nothing();
}
@Override
public Maybe pointer()
{
int column = sourcePosition.getPointer();
return column > 0 ? Maybe.just( Integer.valueOf( column ) ) : Maybe.nothing();
}
@Override
public Maybe pointerSpace()
{
String pointerSpace = null;
int col = sourcePosition.getPointer();
String content = sourcePosition.getLineContent();
if ( col > 0 && content != null )
{
int pointerSpaceLength = Math.min( col, content.length() );
StringBuilder sb = new StringBuilder( pointerSpaceLength );
for ( int i = 0; i < pointerSpaceLength; i++ )
{
sb.append( content.charAt( i ) == '\t' ? '\t' : ' ' );
}
pointerSpace = sb.toString();
}
return pointerSpace != null ? Maybe.just( pointerSpace ) : Maybe.nothing();
}
@Override
public Maybe sourcePath()
{
File sourceFile = sourcePosition.getFile();
return sourceFile != null ? Maybe.just( sourceFile.getAbsolutePath() ) : Maybe.nothing();
}
@Override
public Maybe sourceFile()
{
File sourceFile = sourcePosition.getFile();
return sourceFile != null ? Maybe.just( sourceFile ) : Maybe.nothing();
}
}