Add proposedGrammars method to OakDocument

This returns an array of grammars that can be used with the receiver, either matched via file type extension or first line.
This commit is contained in:
Allan Odgaard
2016-06-29 11:26:28 +02:00
parent adef9fbbbc
commit 15d798dde8
3 changed files with 38 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ PUBLIC extern NSString* OakDocumentMarksDidChangeNotification;
PUBLIC extern NSString* OakDocumentDidSaveNotification;
PUBLIC extern NSString* OakDocumentWillCloseNotification;
@class BundleGrammar;
PUBLIC @interface OakDocument : NSObject
+ (instancetype)documentWithPath:(NSString*)aPath;
+ (instancetype)documentWithData:(NSData*)someData fileType:(NSString*)aFileType customName:(NSString*)aName;
@@ -44,6 +46,8 @@ PUBLIC @interface OakDocument : NSObject
- (void)enumerateByteRangesUsingBlock:(void(^)(char const* bytes, NSRange byteRange, BOOL* stop))block;
@property (nonatomic) NSString* content;
- (NSArray<BundleGrammar*>*)proposedGrammars;
@property (nonatomic, readonly) BOOL canUndo;
@property (nonatomic, readonly) BOOL canRedo;

View File

@@ -7,6 +7,7 @@
#import <OakFoundation/OakFoundation.h>
#import <OakFoundation/NSString Additions.h>
#import <OakAppKit/OakAppKit.h>
#import <BundlesManager/BundlesManager.h>
#import <cf/run_loop.h>
#import <ns/ns.h>
#import <settings/settings.h>
@@ -905,6 +906,38 @@ private:
- (ng::buffer_t&)buffer { return *_buffer; }
- (ng::undo_manager_t&)undoManager { return *_undoManager; }
- (NSArray<BundleGrammar*>*)proposedGrammars
{
NSMutableArray* res = [NSMutableArray array];
std::string const firstLine = _buffer ? _buffer->substr(_buffer->begin(0), std::min<size_t>(_buffer->eol(0), 2048)) : NULL_STR;
std::string const path = to_s(_virtualPath ?: _path);
for(Bundle* bundle in [BundlesManager sharedInstance].bundles)
{
for(BundleGrammar* grammar in bundle.grammars)
{
if(firstLine != NULL_STR && grammar.firstLineMatch && regexp::search(to_s(grammar.firstLineMatch), firstLine))
{
[res addObject:grammar];
break;
}
else if(path != NULL_STR)
{
for(NSString* ext in grammar.filePatterns)
{
if(path::rank(path, to_s(ext)))
{
[res addObject:grammar];
break;
}
}
}
}
}
return res;
}
- (void)enumerateSymbolsUsingBlock:(void(^)(text::pos_t const& pos, NSString* symbol))block
{
if(self.isOpen && _buffer)

View File

@@ -1,5 +1,5 @@
TESTS = tests/t_*.cc
SOURCES = src/*.{mm,cc}
EXPORT = src/OakDocument.h src/document.h src/collection.h
LINK += OakAppKit OakFoundation OakSystem authorization buffer bundles cf command crash encoding file io layout network ns plist regexp scope selection settings text theme undo
LINK += BundlesManager OakAppKit OakFoundation OakSystem authorization buffer bundles cf command crash encoding file io layout network ns plist regexp scope selection settings text theme undo
FRAMEWORKS = ApplicationServices