forked from qt-creator/qt-creator
		
	1 struct Foo { int member; };
 2 void f(Foo *foo)
 3 {
 4     foo.<REQUEST COMPLETION> // correct '.' to '->' and provide results
 5 }
The preferred approach would be to check if "foo" in line 4 is of
pointer type, but there is no suitable cursor (only CompoundStmt) at
that position since the code is usually not yet parsed and thus invalid.
Thus, just run the completion as is. If there are not any results for a
dot completion, re-run the completion with "." exchanged by "->". This
approach is inherently slower than the preferred approach implemented in
the built-in code model.
The following rare cases are not handled:
 1) Requesting completion after white space:
      Works: foo.<COMPLETE HERE>
      Fails: foo. <COMPLETE HERE>
 2) Opening a file and requesting completion (ctrl+space) without prior
    editing. No editing before triggering completion means that no
    unsaved file is generated on the backend side, which is a
    requirement for the correction.
Task-number: QTCREATORBUG-11581
Change-Id: I6bc8e8594778774ab342755fdb01a8a3e5c52ba0
Reviewed-by: Marco Bubke <marco.bubke@theqtcompany.com>
		
	
		
			
				
	
	
		
			88 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/****************************************************************************
 | 
						|
**
 | 
						|
** Copyright (C) 2016 The Qt Company Ltd.
 | 
						|
** Contact: https://www.qt.io/licensing/
 | 
						|
**
 | 
						|
** This file is part of Qt Creator.
 | 
						|
**
 | 
						|
** Commercial License Usage
 | 
						|
** Licensees holding valid commercial Qt licenses may use this file in
 | 
						|
** accordance with the commercial license agreement provided with the
 | 
						|
** Software or, alternatively, in accordance with the terms contained in
 | 
						|
** a written agreement between you and The Qt Company. For licensing terms
 | 
						|
** and conditions see https://www.qt.io/terms-conditions. For further
 | 
						|
** information use the contact form at https://www.qt.io/contact-us.
 | 
						|
**
 | 
						|
** GNU General Public License Usage
 | 
						|
** Alternatively, this file may be used under the terms of the GNU
 | 
						|
** General Public License version 3 as published by the Free Software
 | 
						|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
 | 
						|
** included in the packaging of this file. Please review the following
 | 
						|
** information to ensure the GNU General Public License requirements will
 | 
						|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
 | 
						|
**
 | 
						|
****************************************************************************/
 | 
						|
 | 
						|
#include "temporarymodifiedunsavedfiles.h"
 | 
						|
 | 
						|
#include <cstring>
 | 
						|
 | 
						|
namespace ClangBackEnd {
 | 
						|
 | 
						|
TemporaryModifiedUnsavedFiles::TemporaryModifiedUnsavedFiles(
 | 
						|
        const std::vector<CXUnsavedFile> &unsavedFilesVector)
 | 
						|
    : m_unsavedFileVector(unsavedFilesVector)
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
bool TemporaryModifiedUnsavedFiles::replaceInFile(const Utf8String &filePath,
 | 
						|
                                                  uint offset,
 | 
						|
                                                  uint length,
 | 
						|
                                                  const Utf8String &replacement)
 | 
						|
{
 | 
						|
    const auto isMatchingFile = [filePath] (const CXUnsavedFile &unsavedFile) {
 | 
						|
        return std::strcmp(unsavedFile.Filename, filePath.constData()) == 0;
 | 
						|
    };
 | 
						|
    const auto unsavedFileIterator = std::find_if(m_unsavedFileVector.begin(),
 | 
						|
                                                  m_unsavedFileVector.end(),
 | 
						|
                                                  isMatchingFile);
 | 
						|
 | 
						|
    if (unsavedFileIterator == m_unsavedFileVector.end())
 | 
						|
        return false;
 | 
						|
 | 
						|
    return replaceInFile_internal(*unsavedFileIterator, offset, length, replacement);
 | 
						|
}
 | 
						|
 | 
						|
CXUnsavedFile TemporaryModifiedUnsavedFiles::cxUnsavedFileAt(uint index)
 | 
						|
{
 | 
						|
    return m_unsavedFileVector[index];
 | 
						|
}
 | 
						|
 | 
						|
CXUnsavedFile *TemporaryModifiedUnsavedFiles::cxUnsavedFiles()
 | 
						|
{
 | 
						|
    return m_unsavedFileVector.data();
 | 
						|
}
 | 
						|
 | 
						|
uint TemporaryModifiedUnsavedFiles::count()
 | 
						|
{
 | 
						|
    return uint(m_unsavedFileVector.size());
 | 
						|
}
 | 
						|
 | 
						|
bool TemporaryModifiedUnsavedFiles::replaceInFile_internal(CXUnsavedFile &unsavedFile,
 | 
						|
                                                           uint offset,
 | 
						|
                                                           uint length,
 | 
						|
                                                           const Utf8String &replacement)
 | 
						|
{
 | 
						|
    auto modifiedContent = Utf8String::fromUtf8(unsavedFile.Contents);
 | 
						|
    modifiedContent.replace(int(offset), int(length), replacement);
 | 
						|
 | 
						|
    unsavedFile.Contents = modifiedContent.constData();
 | 
						|
    unsavedFile.Length = uint(modifiedContent.byteSize() + 1);
 | 
						|
 | 
						|
    m_modifiedContents.push_back(modifiedContent); // Keep the modified copy.
 | 
						|
 | 
						|
    return true;
 | 
						|
}
 | 
						|
 | 
						|
} // namespace ClangBackEnd
 |