QString CodeRepresentation::rangeText(const KTextEditor::Range& range) const { Q_ASSERT(range.end().line() < lines()); //Easier for single line ranges which should happen most of the time if(range.onSingleLine()) return QString( line( range.start().line() ).mid( range.start().column(), range.columnWidth() ) ); //Add up al the requested lines QString rangedText = line(range.start().line()).mid(range.start().column()); for(int i = range.start().line() + 1; i <= range.end().line(); ++i) rangedText += '\n' + ((i == range.end().line()) ? line(i).left(range.end().column()) : line(i)); return rangedText; }
QString HighlighterByKate::exportDocument(KTextEditor::Document* note) { QString ret(""); KTextEditor::Range range; range = note->documentRange(); KTextEditor::HighlightInterface* hiface = qobject_cast<KTextEditor::HighlightInterface*>(note); const KTextEditor::Attribute::Ptr noAttrib(0); for (int i = 0; i < note->lines(); ++i) { QString content(""); const QString &line = note->line(i); QList<KTextEditor::HighlightInterface::AttributeBlock> attribs; if ( hiface ) { attribs = hiface->lineAttributes(i); } int lineStart = 0; int remainingChars = line.length(); if ( range.onSingleLine() ) { lineStart = range.start().column(); remainingChars = range.columnWidth(); } else if ( i == range.start().line() ) { lineStart = range.start().column(); } else if ( i == range.end().line() ) { remainingChars = range.end().column(); } int handledUntil = lineStart; foreach ( const KTextEditor::HighlightInterface::AttributeBlock& block, attribs ) { // honor (block-) selections if ( block.start + block.length <= lineStart ) { continue; } else if ( block.start >= lineStart + remainingChars ) { break; } int start = qMax(block.start, lineStart); if ( start > handledUntil ) { exportText(content, line.mid( handledUntil, start - handledUntil ), noAttrib ); } int length = qMin(block.length, remainingChars); exportText(content, line.mid( start, length ), block.attribute); handledUntil = start + length; } if ( handledUntil < lineStart + remainingChars ) { exportText(content, line.mid( handledUntil, remainingChars ), noAttrib ); } if (i != range.end().line()) { ret.append(content.isEmpty() ? "\n" : content); ret.append("\n"); } } return ret; }
void ExporterPluginView::exportData(const bool useSelection, QTextStream &output) { const KTextEditor::Range range = useSelection ? m_view->selectionRange() : m_view->document()->documentRange(); const bool blockwise = useSelection ? m_view->blockSelection() : false; if ( (blockwise || range.onSingleLine()) && (range.start().column() > range.end().column() ) ) { return; } //outputStream.setEncoding(QTextStream::UnicodeUTF8); output.setCodec(QTextCodec::codecForName("UTF-8")); ///TODO: add more exporters AbstractExporter* exporter; exporter = new HTMLExporter(m_view, output, !useSelection); KTextEditor::HighlightInterface* hiface = qobject_cast<KTextEditor::HighlightInterface*>(m_view->document()); const KTextEditor::Attribute::Ptr noAttrib(0); for (int i = range.start().line(); (i <= range.end().line()) && (i < m_view->document()->lines()); ++i) { const QString &line = m_view->document()->line(i); QList<KTextEditor::HighlightInterface::AttributeBlock> attribs; if ( hiface ) { attribs = hiface->lineAttributes(i); } int lineStart = 0; int remainingChars = line.length(); if ( blockwise || range.onSingleLine() ) { lineStart = range.start().column(); remainingChars = range.columnWidth(); } else if ( i == range.start().line() ) { lineStart = range.start().column(); } else if ( i == range.end().line() ) { remainingChars = range.end().column(); } int handledUntil = lineStart; foreach ( const KTextEditor::HighlightInterface::AttributeBlock& block, attribs ) { // honor (block-) selections if ( block.start + block.length <= lineStart ) { continue; } else if ( block.start >= lineStart + remainingChars ) { break; } int start = qMax(block.start, lineStart); if ( start > handledUntil ) { exporter->exportText( line.mid( handledUntil, start - handledUntil ), noAttrib ); } int length = qMin(block.length, remainingChars); exporter->exportText( line.mid( start, length ), block.attribute); handledUntil = start + length; } if ( handledUntil < lineStart + remainingChars ) { exporter->exportText( line.mid( handledUntil, remainingChars ), noAttrib ); } exporter->closeLine(i == range.end().line()); } delete exporter; output.flush(); }