@@ -17,6 +17,7 @@ limitations under the License.
1717import Range from "./range" ;
1818import { Part , Type } from "./parts" ;
1919import { Formatting } from "../components/views/rooms/MessageComposerFormatBar" ;
20+ import { longestBacktickSequence } from './deserialize' ;
2021
2122/**
2223 * Some common queries and transformations on the editor model
@@ -182,11 +183,13 @@ export function formatRangeAsCode(range: Range): void {
182183 const hasBlockFormatting = ( range . length > 0 )
183184 && range . text . startsWith ( "```" )
184185 && range . text . endsWith ( "```" ) ;
186+ const hasBacktick = ( range . text . includes ( "`" ) )
187+ && ! range . text . startsWith ( "`" )
188+ && ! range . text . endsWith ( "`" ) ;
185189
186190 const needsBlockFormatting = parts . some ( p => p . type === Type . Newline ) ;
187191
188192 if ( hasBlockFormatting ) {
189- // Remove previously pushed backticks and new lines
190193 parts . shift ( ) ;
191194 parts . pop ( ) ;
192195 if ( parts [ 0 ] ?. text === "\n" && parts [ parts . length - 1 ] ?. text === "\n" ) {
@@ -205,8 +208,13 @@ export function formatRangeAsCode(range: Range): void {
205208 parts . push ( partCreator . newline ( ) ) ;
206209 }
207210 } else {
208- toggleInlineFormat ( range , "`" ) ;
209- return ;
211+ if ( hasBacktick ) {
212+ parts . unshift ( partCreator . plain ( "`" . repeat ( longestBacktickSequence ( range . text ) + 1 ) ) ) ;
213+ parts . push ( partCreator . plain ( "`" . repeat ( longestBacktickSequence ( range . text ) + 1 ) ) ) ;
214+ } else {
215+ toggleInlineFormat ( range , "`" ) ;
216+ return ;
217+ }
210218 }
211219
212220 replaceRangeAndExpandSelection ( range , parts ) ;
@@ -240,6 +248,7 @@ export function toggleInlineFormat(range: Range, prefix: string, suffix = prefix
240248 // compute paragraph [start, end] indexes
241249 const paragraphIndexes = [ ] ;
242250 let startIndex = 0 ;
251+
243252 // start at i=2 because we look at i and up to two parts behind to detect paragraph breaks at their end
244253 for ( let i = 2 ; i < parts . length ; i ++ ) {
245254 // paragraph breaks can be denoted in a multitude of ways,
0 commit comments