void BackgroundParser::parse()
{
   KTextEditor::EditInterface *ied = 0;
  ied = 
    dynamic_cast<KTextEditor::EditInterface*>(m_ownerDoc->view()->document());
  if(ied) {
    doParse(ied->text());
  }
}
示例#2
0
/**
 * Returns a the complete word defined by the current cursor position.
 * Attempts to extract a valid C symbol from the location of the cursor, by
 * starting at the current line and column, and looking forward and backward
 * for non-symbol characters.
 * @return	A C symbol under the cursor, if any, or QString::null otherwise
 */
QString EditorPage::getWordUnderCursor(uint* pPosInWord)
{
	KTextEditor::ViewCursorInterface* pCursor;
	KTextEditor::EditInterface* pEditIf;
	QString sLine;
	uint nLine, nCol, nFrom, nTo, nLast, nLength;
	QChar ch;

	// Get a cursor object
	pCursor = dynamic_cast<KTextEditor::ViewCursorInterface*>(m_pView);
	if (pCursor == NULL)
		return QString::null;

	// Get a pointer to the edit interface	
	pEditIf = dynamic_cast<KTextEditor::EditInterface*>(m_pDoc);
	if (!pEditIf)
		return QString::null;
	
	// Get the line on which the cursor is positioned
	pCursor->cursorPositionReal(&nLine, &nCol);
	sLine = pEditIf->textLine(nLine);
	
	// Find the beginning of the current word
	for (nFrom = nCol; nFrom > 0;) {
		ch = sLine.at(nFrom - 1);
		if (!ch.isLetter() && !ch.isDigit() && ch != '_')
			break;
		
		nFrom--;
	}
	
	// Find the end of the current word
	nLast = sLine.length();
	for (nTo = nCol; nTo < nLast;) {
		ch = sLine.at(nTo);
		if (!ch.isLetter() && !ch.isDigit() && ch != '_')
			break;
		
		nTo++;
	}
	
	// Mark empty words
	nLength = nTo - nFrom;
	if (nLength == 0)
		return QString::null;
	
	// Return the in-word position, if required
	if (pPosInWord != NULL)
		*pPosInWord = nCol - nFrom;
	
	// Extract the word under the cursor from the entire line
	return sLine.mid(nFrom, nLength);
}
	virtual QString contents( const QString& fileName )
	{
		QString contents = QString::null;
		
		if ( !m_readFromDisk )
		{
			m_deadlock.unlock();
			// GET LOCK
			kapp->lock ();
			
			//kdDebug(9007) << "-------> kapp locked" << endl;
			
			QPtrList<KParts::Part> parts( *m_cppSupport->partController() ->parts() );
			QPtrListIterator<KParts::Part> it( parts );
			while ( it.current() )
			{
				KTextEditor::Document * doc = dynamic_cast<KTextEditor::Document*>( it.current() );
				++it;
				
				KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( doc );
				if ( !doc || !editIface || doc->url().path() != fileName )
					continue;
				
				contents = QString( editIface->text().ascii() ); // deep copy
				
				//kdDebug(9007) << "-------> kapp unlocked" << endl;
				
				break;
			}

			// RELEASE LOCK
			kapp->unlock();
			m_deadlock.lock();
			//kdDebug(9007) << "-------> kapp unlocked" << endl;
		}
		
		if( m_readFromDisk || contents == QString::null )
		{
			QFile f( fileName );
			if ( f.open( IO_ReadOnly ) )
			{
				QTextStream stream( &f );
				contents = stream.read();
				f.close();
			}
		}
		
		return contents;
	}
    virtual QString contents( const QString& fileName )
    {
	if( !m_readFromDisk ){
	    //kdDebug(9013) << "-------> kapp is locked = " << kapp->locked() << endl;
	    bool needToLock = kapp->locked() == false;

	    if( needToLock )
		kapp->lock();

	    //kdDebug(9013) << "-------> kapp locked" << endl;

	    QPtrList<KParts::Part> parts( *m_javaSupport->partController()->parts() );
	    QPtrListIterator<KParts::Part> it( parts );
	    while( it.current() ){
		KTextEditor::Document* doc = dynamic_cast<KTextEditor::Document*>( it.current() );
		++it;

		KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( doc );
		if( !doc || !editIface || doc->url().path() != fileName )
		    continue;

		QString contents = QString( editIface->text().ascii() ); // deep copy

		if( needToLock )
		    kapp->unlock();

		//kdDebug(9013) << "-------> kapp unlocked" << endl;

		return contents;
	    }

	    if( needToLock )
		kapp->unlock();
	    //kdDebug(9013) << "-------> kapp unlocked" << endl;
	}

	QFile f( fileName );
	QTextStream stream( &f );
	if( f.open(IO_ReadOnly) ){
	    QString contents = stream.read();
	    f.close();
	    return contents;
	}

	return QString::null;
    }
示例#5
0
/**
 * Returns the contents of the requested line.
 * Truncates the leading and trailing white spaces.
 * @param	nLine	The line number
 * @return	The text of the requested line, if successful, QString::null
 *			otherwise
 */
QString EditorPage::getLineContents(uint nLine)
{
	KTextEditor::EditInterface* pEditIf;
	QString sLine;

	// Cannot accept line 0
	if (nLine == 0)
		return QString::null;
	
	// Get a pointer to the edit interface
	pEditIf = dynamic_cast<KTextEditor::EditInterface*>(m_pDoc);
	if (!pEditIf)
		return QString::null;

	// Get the line on which the cursor is positioned
	sLine = pEditIf->textLine(nLine - 1);
	return sLine.stripWhiteSpace();
}
示例#6
0
/*!
    \fn SnippetWidget::insertIntoActiveView(QString text)
    Inserts the parameter text into the activ view
 */
void SnippetWidget::insertIntoActiveView(QString text)
{
  //get the interfaces for the KTexteditor parts
  KTextEditor::ViewCursorInterface *cursorIface
	= dynamic_cast<KTextEditor::ViewCursorInterface*>(m_part->partController()->activeWidget());

  if (!cursorIface)
    return;

  KTextEditor::EditInterface* editIface
	= dynamic_cast<KTextEditor::EditInterface*>( m_part->partController()->activePart() );

  if (!editIface)
    return;

  uint line, col;
  cursorIface->cursorPositionReal(&line, &col);

  editIface->insertText( line, col , text );
}
示例#7
0
QStringList PHPFile::readFromEditor()
{
   QStringList contents;

   kapp->lock();
   QPtrList<KParts::Part> parts( *m_part->partController()->parts() );
   QPtrListIterator<KParts::Part> it( parts );
   while( it.current() ){
      KTextEditor::Document* doc = dynamic_cast<KTextEditor::Document*>( it.current() );
      ++it;

      KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( doc );
      if ( !doc || !editIface || doc->url().path() != fileName() )
         continue;

      contents = QStringList::split("\n", editIface->text().ascii(), true);
      break;
   }
   kapp->unlock();

   return contents;
}
示例#8
0
void AddMethodDialog::accept()
{
	m_cppSupport->partController() ->editDocument( KURL( m_klass->fileName() ) );
	KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( m_cppSupport->partController() ->activePart() );
	if ( !editIface )
	{
		/// @todo show messagebox
		QDialog::accept();
		return ;
	}

	int line, column;
	m_klass->getEndPosition( &line, &column );

	// compute the insertion point map
	QMap<QString, QPair<int, int> > points;
	QStringList accessList;

	const FunctionList functionList = m_klass->functionList();
	for ( FunctionList::ConstIterator it = functionList.begin(); it != functionList.end(); ++it )
	{
		int funEndLine, funEndColumn;
		( *it ) ->getEndPosition( &funEndLine, &funEndColumn );
		QString access = accessID( *it );
		QPair<int, int> funEndPoint = qMakePair( funEndLine, funEndColumn );

		if ( !points.contains( access ) || points[ access ] < funEndPoint )
		{
			accessList.remove( access );
			accessList.push_back( access ); // move 'access' at the end of the list

			points[ access ] = funEndPoint;
		}
	}

	int insertedLine = 0;

	accessList += newAccessList( accessList );

	for ( QStringList::iterator it = accessList.begin(); it != accessList.end(); ++it )
	{
		QListViewItem* item = methods->firstChild();
		while ( item )
		{
			QListViewItem * currentItem = item;

			item = item->nextSibling();

			if ( currentItem->text( 1 ) != *it )
				continue;

			QString access = ( *it ).lower();

			bool isInline = currentItem->text( 0 ) == "True";
			QString str = isInline ? functionDefinition( currentItem ) : functionDeclaration( currentItem );

			QPair<int, int> pt;
			if ( points.contains( *it ) )
			{
				pt = points[ *it ];
			}
			else
			{
			str.prepend( access + ":\n" );
				points[ *it ] = qMakePair( line - 1, 0 );
				pt = points[ *it ]; // end of class declaration
			}

			editIface->insertText( pt.first + insertedLine + 1, 0 /*pt.second*/, str );
			insertedLine += str.contains( QChar( '\n' ) );
		}
	}

	m_cppSupport->backgroundParser() ->addFile( m_klass->fileName() );

	QString str;
	QListViewItem* item = methods->firstChild();
	while ( item )
	{
		QListViewItem * currentItem = item;

		item = item->nextSibling();

		QString str = functionDefinition( currentItem );
		if ( str.isEmpty() )
			continue;

		QString implementationFile = currentItem->text( 5 );
		if ( currentItem->text( 0 ) == "True" )
			implementationFile = m_klass->fileName();

		QFileInfo fileInfo( implementationFile );
		if ( !QFile::exists( fileInfo.absFilePath() ) )
		{
			if ( KDevCreateFile * createFileSupp = m_cppSupport->extension<KDevCreateFile>( "KDevelop/CreateFile" ) )
				createFileSupp->createNewFile( fileInfo.extension(), fileInfo.dirPath( true ), fileInfo.baseName() );
		}

		m_cppSupport->partController() ->editDocument( KURL( implementationFile ) );
		editIface = dynamic_cast<KTextEditor::EditInterface*>( m_cppSupport->partController() ->activePart() );
		if ( !editIface )
			continue;

		bool isInline = currentItem->text( 0 ) == "True";
		if ( !isInline )
		{
			editIface->insertLine( editIface->numLines(), QString::fromLatin1( "" ) );
			editIface->insertText( editIface->numLines() - 1, 0, str );
			m_cppSupport->backgroundParser() ->addFile( implementationFile );
		}
	}

	QDialog::accept();
}
示例#9
0
/**
 * Moves the cursor to a given position.
 * @param	nLine	The cursor's new line number
 * @param	nCol	The cursor's new column number
 * @return	true if successful, false otherwise (cursor interface was not
 *			obtained)
 */
bool EditorPage::setCursorPos(uint nLine, uint nCol)
{
	Kate::View* pKateView;
	KTextEditor::ViewCursorInterface* pCursorIf;
	
	// Cannot accept line 0
	if (nLine == 0)
		return false;
	
	// Adjust to 0-based counting
	nLine--;
	nCol--;
		
	// Acquire the view cursor
	pCursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>(m_pView);
	if (pCursorIf == NULL)
		return false;
	
	// NOTE: The following code is a fix to a bug in Kate, which wrongly
	// calculates the column number in setCursorPosition.
	pKateView = dynamic_cast<Kate::View*>(m_pView);
	if (pKateView != NULL) {
		KTextEditor::EditInterface* pEditIf;
		const char* szLine;
		uint nRealCol;
		uint nTabAdjust;
		
		// Get a pointer to the edit interface
		pEditIf = dynamic_cast<KTextEditor::EditInterface*>(m_pDoc);
		if (!pEditIf)
			return false;
		
		nRealCol = 0;
		
		// Check for out of bound line numbers
		if (nLine < pEditIf->numLines()) {
			// Get the contents of the requested line
			szLine = pEditIf->textLine(nLine).latin1();
			
			// Check for empty line
			if (szLine != NULL) {
				// The number of columns which a tab character adds
				nTabAdjust = pKateView->tabWidth() - 1;
				
				// Calculate the real column, based on the tab width
				for (; nRealCol < nCol && szLine[nRealCol] != 0; nRealCol++) {
					if (szLine[nRealCol] == '\t')
						nCol -= nTabAdjust;
				}
			}
		}
		else {
			// Marker set beyond end of file, move to the last line
			nLine = pEditIf->numLines() - 1;
		}
		// Set the cursor position
		pCursorIf->setCursorPositionReal(nLine, nRealCol);
	}
	else {
		// Non-Kate editors: set the cursor position normally
		pCursorIf->setCursorPosition(nLine, nCol);
	}
	
	return true;
}
示例#10
0
void AStylePart::beautifySource()
{
  KTextEditor::EditInterface *iface
      = dynamic_cast<KTextEditor::EditInterface*>(partController()->activePart());
  if (!iface)
    return;

    bool has_selection = false;

  KTextEditor::SelectionInterface *sel_iface
      = dynamic_cast<KTextEditor::SelectionInterface*>(partController()->activePart());
  if (sel_iface && sel_iface->hasSelection())
    has_selection = true;

  //if there is a selection, we only format it.
  ASStringIterator is(has_selection ? sel_iface->selection() : iface->text());
  KDevFormatter formatter(m_project);

  formatter.init(&is);

  QString output;
  QTextStream os(&output, IO_WriteOnly);

  // put the selection back to the same indent level.
  // taking note of the config options.
  unsigned int indentCount=0;
  QString indentWith("");
  if ( has_selection){
  	QString original = sel_iface->selection();
	for (;indentCount<original.length();indentCount++){
		QChar ch = original[indentCount];
		if ( ch.isSpace()){
			if ( ch == QChar('\n') || ch == QChar('\r')){
				indentWith="";
			}
			else{
				indentWith+=original[indentCount];
			}
	  	}
		else{
			break;
		}
	}

	int wsCount = m_project["FillCount"].toInt();
	if (m_project["Fill"].toString() == "Tabs")
	{
		// tabs and wsCount spaces to be a tab
		QString replace;
		for (int i =0;i<wsCount;i++)
			replace+=' ';

		indentWith=indentWith.replace(replace, QChar('\t'));
		indentWith=indentWith.remove(' ');
	} else
	{
		if ( m_project["FillForce"].toBool()){
			//convert tabs to spaces
			QString replace;
			for (int i =0;i<wsCount;i++)
				replace+=' ';

			indentWith=indentWith.replace(QChar('\t'),replace);
		}
	}
  }

  while (formatter.hasMoreLines()){
	  if ( has_selection ){
		  os << indentWith;
	  }
	  os << QString::fromUtf8(formatter.nextLine().c_str()) << endl;
  }

  uint col = 0;
  uint line = 0;

  if(has_selection) //there was a selection, so only change the part of the text related to it
  {
    //remove the final newline character, unless it should be there
    if ( !sel_iface->selection().endsWith( "\n" ) )
      output.setLength(output.length()-1);

    sel_iface->removeSelectedText();
    cursorPos( partController()->activePart(), &col, &line );
    iface->insertText( col, line, output);

    return;
  }

  cursorPos( partController()->activePart(), &col, &line );

  iface->setText( output );

  setCursorPos( partController()->activePart(), col, line );
}
示例#11
0
void KDataToolPluginView::aboutToShow()
{
	kdDebug()<<"KTextEditor::KDataToolPluginView::aboutToShow"<<endl;
	QString word;
	m_singleWord = false;
	m_wordUnderCursor = QString::null;

	// unplug old actions, if any:
	KAction *ac;
	for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
		m_menu->remove(ac);
	}
	if (m_notAvailable) {
		m_menu->remove(m_notAvailable);
		delete m_notAvailable;
		m_notAvailable=0;
	}
	if ( selectionInterface(m_view->document())->hasSelection() )
	{
		word = selectionInterface(m_view->document())->selection();
		if ( word.find(' ') == -1 && word.find('\t') == -1 && word.find('\n') == -1 )
			m_singleWord = true;
		else
			m_singleWord = false;
	} else {
		// No selection -> use word under cursor
		KTextEditor::EditInterface *ei;
		KTextEditor::ViewCursorInterface *ci;
		KTextEditor::View *v = (KTextEditor::View*)m_view; 
		ei = KTextEditor::editInterface(v->document());
		ci = KTextEditor::viewCursorInterface(v);
		uint line, col;
		ci->cursorPositionReal(&line, &col);
		QString tmp_line = ei->textLine(line);
		m_wordUnderCursor = "";
		// find begin of word:
		m_singleWord_start = 0;
		for(int i = col; i >= 0; i--) {
			QChar ch = tmp_line.at(i);
			if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
			{
				m_singleWord_start = i+1;
				break;
			}
			m_wordUnderCursor = ch + m_wordUnderCursor;
		}
		// find end of word:
		m_singleWord_end = tmp_line.length();
		for(uint i = col+1; i < tmp_line.length(); i++) {
			QChar ch = tmp_line.at(i);
			if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
			{
				m_singleWord_end = i;
				break;
			}
			m_wordUnderCursor += ch;
		}
		if( ! m_wordUnderCursor.isEmpty() )
		{
			m_singleWord = true;
			m_singleWord_line = line;
		} else {
			m_notAvailable = new KAction(i18n("(not available)"), QString::null, 0, this, 
					SLOT(slotNotAvailable()), actionCollection(),"dt_n_av");
			m_menu->insert(m_notAvailable);
			return;
		}
	}

	KInstance *inst=instance();

	QValueList<KDataToolInfo> tools;
	tools += KDataToolInfo::query( "QString", "text/plain", inst );
	if( m_singleWord )
		tools += KDataToolInfo::query( "QString", "application/x-singleword", inst );

	m_actionList = KDataToolAction::dataToolActionList( tools, this,
		SLOT( slotToolActivated( const KDataToolInfo &, const QString & ) ) );

	for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
		m_menu->insert(ac);
	}

	if( m_actionList.isEmpty() ) {
		m_notAvailable = new KAction(i18n("(not available)"), QString::null, 0, this,
			SLOT(slotNotAvailable()), actionCollection(),"dt_n_av");
		m_menu->insert(m_notAvailable);
	}
}