Skip to content

Commit

Permalink
Merge pull request ycm-core#104 from SolaWing/waitForMerge
Browse files Browse the repository at this point in the history
fixed complete in objective-c.
  • Loading branch information
Valloric committed Apr 8, 2015
2 parents 72dfe86 + 443b99f commit 7e03b9a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
49 changes: 41 additions & 8 deletions cpp/ycm/ClangCompleter/CompletionData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ CompletionKind CursorKindToCompletionKind( CXCursorKind kind ) {

case CXCursor_ClassDecl:
case CXCursor_ClassTemplate:
case CXCursor_ObjCInterfaceDecl:
case CXCursor_ObjCImplementationDecl:
return CLASS;

case CXCursor_EnumDecl:
Expand All @@ -43,6 +45,9 @@ CompletionKind CursorKindToCompletionKind( CXCursorKind kind ) {
return TYPE;

case CXCursor_FieldDecl:
case CXCursor_ObjCIvarDecl:
case CXCursor_ObjCPropertyDecl:
case CXCursor_EnumConstantDecl:
return MEMBER;

case CXCursor_FunctionDecl:
Expand All @@ -51,6 +56,8 @@ CompletionKind CursorKindToCompletionKind( CXCursorKind kind ) {
case CXCursor_ConversionFunction:
case CXCursor_Constructor:
case CXCursor_Destructor:
case CXCursor_ObjCClassMethodDecl:
case CXCursor_ObjCInstanceMethodDecl:
return FUNCTION;

case CXCursor_VarDecl:
Expand Down Expand Up @@ -90,7 +97,8 @@ bool IsMainCompletionTextInfo( CXCompletionChunkKind kind ) {
kind == CXCompletionChunk_SemiColon ||
kind == CXCompletionChunk_Equal ||
kind == CXCompletionChunk_Informative ||
kind == CXCompletionChunk_HorizontalSpace;
kind == CXCompletionChunk_HorizontalSpace ||
kind == CXCompletionChunk_Text;

}

Expand Down Expand Up @@ -161,12 +169,18 @@ CompletionData::CompletionData( const CXCompletionResult &completion_result ) {
uint num_chunks = clang_getNumCompletionChunks( completion_string );
bool saw_left_paren = false;
bool saw_function_params = false;
bool saw_placeholder = false;

for ( uint j = 0; j < num_chunks; ++j ) {
ExtractDataFromChunk( completion_string,
j,
saw_left_paren,
saw_function_params );
saw_function_params,
saw_placeholder );
}
if ( original_string_.size() > 0 &&
original_string_[ original_string_.size() - 1 ] == '(' ) {
original_string_.erase( original_string_.size() - 1, 1 );
}

kind_ = CursorKindToCompletionKind( completion_result.CursorKind );
Expand Down Expand Up @@ -194,7 +208,8 @@ CompletionData::CompletionData( const CXCompletionResult &completion_result ) {
void CompletionData::ExtractDataFromChunk( CXCompletionString completion_string,
uint chunk_num,
bool &saw_left_paren,
bool &saw_function_params ) {
bool &saw_function_params,
bool &saw_placeholder ) {
CXCompletionChunkKind kind = clang_getCompletionChunkKind(
completion_string, chunk_num );

Expand All @@ -212,6 +227,8 @@ void CompletionData::ExtractDataFromChunk( CXCompletionString completion_string,
}

else if ( saw_function_params && kind == CXCompletionChunk_RightParen ) {
saw_left_paren = false;
saw_function_params = false; // fix objc have more than one ()
everything_except_return_type_.append( " " );
}

Expand All @@ -226,11 +243,27 @@ void CompletionData::ExtractDataFromChunk( CXCompletionString completion_string,
}
}

if ( kind == CXCompletionChunk_ResultType )
return_type_ = ChunkToString( completion_string, chunk_num );

if ( kind == CXCompletionChunk_TypedText )
original_string_ = ChunkToString( completion_string, chunk_num );
switch ( kind ) {
case CXCompletionChunk_ResultType:
return_type_ = ChunkToString( completion_string, chunk_num );
break;
case CXCompletionChunk_Placeholder:
saw_placeholder = true;
break;
case CXCompletionChunk_TypedText:
case CXCompletionChunk_Text:
// need to add paren to insert string
// when implementing inherited methods or declared methods in objc.
case CXCompletionChunk_LeftParen:
case CXCompletionChunk_RightParen:
case CXCompletionChunk_HorizontalSpace:
if ( !saw_placeholder ) {
original_string_ += ChunkToString( completion_string, chunk_num );
}
break;
default:
break;
}
}

} // namespace YouCompleteMe
3 changes: 2 additions & 1 deletion cpp/ycm/ClangCompleter/CompletionData.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ struct CompletionData {
void ExtractDataFromChunk( CXCompletionString completion_string,
uint chunk_num,
bool &saw_left_paren,
bool &saw_function_params );
bool &saw_function_params,
bool &saw_placeholder);
};

} // namespace YouCompleteMe
Expand Down
29 changes: 29 additions & 0 deletions cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ TEST( ClangCompleterTest, CandidatesForLocationInFile ) {

EXPECT_TRUE( !completions.empty() );
}

TEST( ClangCompleterTest, CandidatesObjCForLocationInFile ) {
ClangCompleter completer;
std::vector< CompletionData > completions =
completer.CandidatesForLocationInFile(
PathToTestFile( "SWObject.m" ).string(),
6,
16,
std::vector< UnsavedFile >(),
std::vector< std::string >{"-x", "objective-c"} );

EXPECT_TRUE( !completions.empty() && completions[0].TextToInsertInBuffer() == "withArg2:");
}

TEST( ClangCompleterTest, CandidatesObjCFuncForLocationInFile ) {
ClangCompleter completer;
std::vector< CompletionData > completions =
completer.CandidatesForLocationInFile(
PathToTestFile( "SWObject.m" ).string(),
9,
3,
std::vector< UnsavedFile >(),
std::vector< std::string >{"-x", "objective-c"} );

EXPECT_TRUE( !completions.empty() &&
completions[0].TextToInsertInBuffer()
== "(void)test:(int)arg1 withArg2:(int)arg2 withArg3:(int)arg3");
}



TEST( ClangCompleterTest, GetDefinitionLocation ) {
Expand Down
7 changes: 7 additions & 0 deletions cpp/ycm/tests/testdata/SWObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@interface SWObject
/** testFunc
*
* a detail description
*/
- (void)test:(int)arg1 withArg2:(int)arg2 withArg3:(int)arg3;
@end
11 changes: 11 additions & 0 deletions cpp/ycm/tests/testdata/SWObject.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import "SWObject.h"

@implementation SWObject

- (void)test {
[self test:0 /*complete at here*/]
}

- /*complete at here*/

@end

0 comments on commit 7e03b9a

Please sign in to comment.