mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 06:32:44 +00:00 
			
		
		
		
	GTextEditor: Support splitting lines at the cursor with the Return key.
This commit is contained in:
		
							parent
							
								
									a64b71fb3d
								
							
						
					
					
						commit
						43d56b6f3a
					
				
					 3 changed files with 30 additions and 6 deletions
				
			
		
							
								
								
									
										20
									
								
								AK/Vector.h
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								AK/Vector.h
									
										
									
									
									
								
							|  | @ -252,12 +252,22 @@ public: | ||||||
| 
 | 
 | ||||||
|     void resize(ssize_t new_size) |     void resize(ssize_t new_size) | ||||||
|     { |     { | ||||||
|         ASSERT(new_size >= size()); |         if (new_size == size()) | ||||||
|         if (!new_size) |  | ||||||
|             return; |             return; | ||||||
|         ensure_capacity(new_size); | 
 | ||||||
|         for (ssize_t i = size(); i < new_size; ++i) |         if (!new_size) { | ||||||
|             new (m_impl->slot(i)) T; |             clear(); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (new_size > size()) { | ||||||
|  |             ensure_capacity(new_size); | ||||||
|  |             for (ssize_t i = size(); i < new_size; ++i) | ||||||
|  |                 new (m_impl->slot(i)) T; | ||||||
|  |         } else { | ||||||
|  |             for (int i = new_size; i < size(); ++i) | ||||||
|  |                 m_impl->at(i).~T(); | ||||||
|  |         } | ||||||
|         m_impl->m_size = new_size; |         m_impl->m_size = new_size; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -240,10 +240,17 @@ void GTextEditor::insert_at_cursor(char ch) | ||||||
|         if (at_tail || at_head) { |         if (at_tail || at_head) { | ||||||
|             m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>()); |             m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>()); | ||||||
|             update_scrollbar_ranges(); |             update_scrollbar_ranges(); | ||||||
|             set_cursor(m_cursor.line() + 1, 0); |  | ||||||
|             update(); |             update(); | ||||||
|  |             set_cursor(m_cursor.line() + 1, 0); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |         auto new_line = make<Line>(); | ||||||
|  |         new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column()); | ||||||
|  |         current_line().truncate(m_cursor.column()); | ||||||
|  |         m_lines.insert(m_cursor.line() + 1, move(new_line)); | ||||||
|  |         update_scrollbar_ranges(); | ||||||
|  |         update(); | ||||||
|  |         set_cursor(m_cursor.line() + 1, 0); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|     current_line().insert(m_cursor.column(), ch); |     current_line().insert(m_cursor.column(), ch); | ||||||
|  | @ -414,3 +421,9 @@ void GTextEditor::Line::remove(int index) | ||||||
|         m_text.remove(index); |         m_text.remove(index); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | void GTextEditor::Line::truncate(int length) | ||||||
|  | { | ||||||
|  |     m_text.resize(length + 1); | ||||||
|  |     m_text.last() = 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -73,6 +73,7 @@ private: | ||||||
|         void insert(int index, char); |         void insert(int index, char); | ||||||
|         void remove(int index); |         void remove(int index); | ||||||
|         void append(const char*, int); |         void append(const char*, int); | ||||||
|  |         void truncate(int length); | ||||||
| 
 | 
 | ||||||
|     private: |     private: | ||||||
|         // NOTE: This vector is null terminated.
 |         // NOTE: This vector is null terminated.
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling